Spring Boot Exception Handling — HandlerExceptionResolver

Error and Exception handling is essential for a web application and proper responses with reasonable details are required for better debugging and user experience. Spring Boot provides a /error mapping that handles all errors in a sensible way, and it is registered as a “global” error page in the servlet container. If you are implementing a rest API or a web application with a UI template you will get a proper JSON or HTML view rendered. You will have the option to change this template using different methods.

There are different techniques to handle exceptions at the controller level as well as application level. Using these methods you are able to prepare the proper response with the proper status.

In this article, we will focus on HandlerExceptionResolver and share some examples with this Interface to handle exceptions for web applications.

Getting Started

To start you will need to implement the resolveException method inside the HandlerExceptionResolver interface. The return type of this method is a ModelAndView object and you will have access to the request, response, and exception object instances.

@Component
class RestResponseStatusExceptionResolver implements HandlerExceptionResolver {

@Override
public ModelAndView resolveException(
HttpServletRequest request,
HttpServletResponse response,
Object object,
Exception exception) {
ModelAndView model = new ModelAndView();
model.setView(new MappingJackson2JsonView());
model.addObject("exception", exception.getMessage());
return model;
}

}

If you want to have different logic for different types of exceptions you can use the instanceof keyword to check the type of exception and show the proper error message and view.

...
if
( exception instanceof ServiceDownTimeException ){
ModelAndView model = new ModelAndView();
model.setView(new MappingJackson2JsonView());
model.addObject("exception", exception.getMessage());
return model;
}
...

If you want to have the view and not a JSON response you can set the view HTML file name in the ModelAndView object.

model.setView("errors/service-down-time.html");

Finally, to have this HandlerExceptionResolver handler start working you will need to add it to the resolvers list as part of WebMvcConfigurer.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> resolvers) {
resolvers.add(0, new RestResponseStatusExceptionResolver());
}

}

After setting everything if any exception happened and you had the proper handler configured you will get a response like this:

{
"serviceDownTimeException": {
"cause": null,
"stackTrace": [],
"localizedMessage": "1-2 AM is service downtime!",
"message": "1-2 AM is service downtime!",
"suppressed": []
}
}

The Example code is available on GitHub. You can learn more about the other methods of handling exceptions in the following articles:

Hope this article helped you and please support me with your applauding 👏 for the story. if you don’t know how it is just like this:

Or buy me a coffee here!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Farzin Pashaee

Farzin Pashaee

Software Engineer at Maybank, AI and ML enthusiastic