且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Spring Boot中无法从REST API返回错误代码和消息

更新时间:2021-12-30 15:24:51

根据您的控制器,它用来引发"BusinessException"异常.但是您尚未实现在Controller Advisor中捕获该异常的方法"GlobalExceptionHandler".测试成功后,请在您的控制器顾问中包含以下方法.

As per your controller it uses to throw "BusinessException" exception. But you have not implemented a method to catch that exception in controller adviser which is "GlobalExceptionHandler ". Please include below method in your controller adviser as test was successful.

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> handleBusinessException(BusinessException businessException ) {

        return new ResponseEntity<>("Your specific error", HttpStatus.NOT_FOUND);
    }

下面是测试结果