Exception Handling for REST API with Spring Boot

Sampath Thennakoon
3 min readSep 24, 2018

--

Exception handling is very important and must have feature for RESTful APIs.

A good error messages help clients to take corrective actions to handle the critical situations in the applications.In this post, we are going to implement exception handling with spring boot latest release.

Why Spring Boot,

Spring boot provides the out of the box white label error handling feature.For example if you run sample default spring boot application you can see default error page for invalid endpoint like this.

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Sep 24 09:27:00 IST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

We are going to customize the default spring boot error message with new meaningful JSON error message. For example,

{“status”:404,”path”:”/test”,”errorMessage”:”No message available”,”timeStamp”:”Mon Sep 24 09:34:27 IST 2018",”trace”:null}

In this error JSON we define attributes in following way.
* The status represents HTTP status code.
* The path field specifies a link for detail information about the error or exception.
* The errorMessage field represents human-readable error message.
* The timeStamp field represents error occurred time.
* The trace section represents error information with complete details which is important for developers.

Spring and Spring Boot provides a number of options for error/exception handling.

1) ExceptionHandler Annotation

This annotation works at the @Controller class level.The issue with the approach is only active for the given controller.The annotation is not global, so we need to implement in each and every controller.

2) @ControllerAdvice Annotation

This annotation supports global Exception handler mechanism.So we can implement the controller exception handling events in a central location.

3) ResponseEntityExceptionHandler

This method can be used with @ControllerAdvice classes.It allows the developer to specify some specific templates of ResponseEntity and return values.

4) @RestControllerAdvice

Spring Boot 1.4 introduced the @RestControllerAdvice annotation for easier exception handling.It is a convenience annotation that is itself annotated with @ControllerAdvice and @ResponseBody.

5) In spring boot implementing the ErrorController and overriding the default whitelabel error template.

In our case Spring Boot by default error page is map with /error URI to BasicErrorController which populates model with error attributes and then returns ‘error’ as the view name to map application defined error pages.

  • The way to override BasicErrorController with our own custom controller which can map to ‘/error’, we can implement ErrorController interface.In following example we have defined the output error response template with in the code.
  • Another way is to implement custom template is defined the ExceptionResponse model object to define the error json output.

When we override the default error template we have the define the server.error.whitelabel.enabled=false in the spring boot project application.properties file.

Total sample project is avalible in my github repository.Feel free to clone and modify repo as you want.

References :

https://gist.github.com/jonikarppinen/662c38fb57a23de61c8b
https://www.javadevjournal.com/spring/exception-handling-for-rest-with-spring/
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
https://www.javainuse.com/spring/boot-exception-handling

--

--

Sampath Thennakoon

Love travel , love programming , experiments and solve problems , curious learner