I need RESTful service to return JSON every time regardless if there is error or not. With normal cases everything goes ok, but when exceptions come I see Whitelabel Error Page .
I tried to resolve this in two ways.
Through @ExceptionHandler inside @Controller class inside the class annotated with @RestController:
@ExceptionHandler(MyCustomException.class)
@ResponseBody
public ErrorResponse handleException(Exception e) {
return new ErrorResponse(5, "Error message");
}
And through special class with @ControllerAdvice annotation:
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(MyCustomException.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleSecurityException(MyCustomException e) {
return new ErrorResponse(5, "Error message");
}
}
In both cases through breakpoints I see that those methods was called, but I still receive Whitelabel Error Page .
So what am I missing or doing wrong?
问题是我忘了为ErrorResponse类实现getter和setter。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.