I am using both the ExceptionAdvice and ResponseStatusException to handle the exception situation in my web application. Now I'd like to log the exception information while throwing the ResponseStatusException in my Controller class.
I can always write the log code near the line that throw the exception in my Controller class:
controllerMethod(){
logger.error("some thing happens here!");
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "some reason");
}
But writing code all over the place is too tedious, In fact, i'd like some pattern that i used in my ExceptionAdvice class:
@ResponseBody
@ExceptionHandler(MyException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
String myExceptionHandler(MyException e){
logger.error("oops!", e);
return "something";
}
However, the ReponseStatusException response that Spring generated has the format which i want to maintain like:
{
"timestamp": "2018-02-01T04:28:32.917+0000",
"status": 400,
"error": "Bad Request",
"message": "Provide correct Actor Id",
"path": "/actor/8/BradPitt"
}
So is there anyway that i can use advice class to log for the ResponseStatusException while still maintaining its generated response, or, on the contrast, using other class to add log ability around all the ReponseStatusException without typing the logger.error everywhere that the exception is thrown?