Spring Boot 3.2.x / Spring Web 6.1.x
In an app with both MVC controllers (returning HTML pages via JSP) and REST controllers, how can I implement global exception handling such that MVC controller methods result in an error page while REST endpoints return non-page responses (400, 404, 500, etc with text or JSON response body)?
I have a @ControllerAdvice
implemented that handles various kinds of exceptions with specific @ExceptionHandler
and @ResponseStatus
annotations. These methods all return a page (eg, "forward:/error"
) as they should for MVC requests.
Here’s part of that class:
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
//...other methods...
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String defaultInternalErrorHandler(Model model, Exception e, HttpServletRequest req) {
UUID errorID = UUID.randomUUID();
log.error(errorID + " | Unhandled exception processing request to " + req.getRequestURI(), e);
model.addAttribute("exception", e);
model.addAttribute("errorID", errorID);
return "forward:/error";
}
}
This is the “fallback” handler method if none of the others handle the exception that’s thrown. This works great for all the MVC controller methods that return pages (HTML).
However, I don’t want REST controller endpoints to return HTML content, they should return (JSON in my case) content with an appropriate HTTP response status (eg, 400 for invalid requests, 500 for server errors, etc).
I’ve tried adding another @RestControllerAdvice
class similar to this:
@RestControllerAdvice
@Slf4j
public class GlobalRestExceptionHandler {
@ExceptionHandler(ServletRequestBindingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handle(ServletRequestBindingException e, HttpServletRequest req) {
defaultInternalErrorHandler(e, req);
return "Invalid Request: " + e.getMessage();
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String defaultInternalErrorHandler(Exception e, HttpServletRequest req) {
UUID errorID = UUID.randomUUID();
log.error(errorID + " | Unhandled exception processing API request to " + req.getRequestURI(), e);
return "Server error: " + e;
}
}
Unfortunately, that class doesn’t seem to be used. Any exception from a REST endpoint is flowing through the @ControllerAdvice
default method and returning the error page.
How can I have the best of both worlds, MVC requests going to the error page and REST endpoints returning REST-friendly (JSON) content?