I have a basic SpringBoot 2.0.4.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
I've created this class to manage the Exceptions:
@ControllerAdvice
@RestControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
public RestResponseEntityExceptionHandler() {
super();
}
// 500
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class, RuntimeException.class })
/*500*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public BodyBuilder handleInternal(final RuntimeException ex, final WebRequest request) {
logger.error("500 Status Code", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
the method Handles the RuntimeException and ArithmeticException extends RuntimeException.
To test it I've created this method ;
GetMapping(path = "/getUsers", consumes = "application/json", produces = "application/json")
public ResponseEntity<List<User>> testErrors(HttpServletRequest request) {
double ss = 3 /0;
return ResponseEntity.ok(userService.findAll());
}
expecting that will only returns a HttpStatus.INTERNAL_SERVER_ERROR but instead it returns"
{"timestamp":"2018-08-31T09:21:21.717+0000","status":500,"error":"Internal Server Error","message":"/ by zero","trace":"java.lang.ArithmeticException: / by zero\n\tat ...
With this method
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class, RuntimeException.class })
/*500*/
public BodyBuilder handleInternal(final RuntimeException ex, final WebRequest request) {
logger.error("500 Status Code", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
}
I have the full trace: {"timestamp":"2018-09-03T07:04:58.803+0000","status":500,"error":"Internal Server Error","message":"/ by zero","trace":"java.lang.ArithmeticException: / by zero\n\tat
and with this one, I don't see anything in the console with curl:
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class, RuntimeException.class })
/*500*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleInternal(final RuntimeException ex, final WebRequest request) {
logger.error("500 Status Code", ex);
}
and this other one, also nothing in the console:
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class, RuntimeException.class })
/*500*/
public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
logger.error("500 Status Code", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}