Exception Handler in Spring Boot
Overview
Every software program can run into an error that developers must handle cautiously. Handling exception is an important part of building resilient and robust applications.
Introduction to Spring Boot’s Default Exception Handling Mechanism
Let's say we have an employee management system. It contains an API to fetch an employee by ID. Employee Service throws the exception EmployeeNotFoundException if an employee is not found.
If we invoke /employee/1, then it should throw EmployeeNotFoundException, which should render some response to the caller of the API. We will get the following response:
By default, spring converts exceptions to an internal server error. Spring boot provides and uses DefaultHandlerExceptionResolver if no explicit handler is defined to handle API exceptions. But the default handler is not the correct choice for the restful application as it converts all the exceptions to "500- Internal server error" response, which does not go with the style of the HTTP and REST.
Our case response should be returned "404 - Not found" because the resource user trying to fetch does not exist. This is where @ExceptionHandler is helpful to handle and return the correct response to the API caller.
Spring Boot Exception Handling Using @ExceptionHandler
We will now see how exception handler in spring boot work with the help of the @ExceptionHandler annotation. @ExceptionHandler annotation gives lots of flexibility when dealing with HTTP exceptions. Its signature allows us to provide a list of exceptions as input and modify HTTP responses as per HTTP and Rest standards.
An @ExceptionHandler can be applied on the same controller class or a separate class with the @ControllerAdvice annotation.
We will now improve our use case with @ExceptionHandling and return the desired response to the user.
In the above example, the method handleEmployeeNotFound accept EmployeeNotFoundException as input and convert the response to HTTP status 404 and body to message from the exception. Now invoking the same API should give the below response:
Internal working of @ExceptionHandler
In spring boot, all the exception handler extends abstract AbstractHandlerExceptionResolver with below concrete implementations.
- DefaultHandlerExceptionResolver - Default resolver if no other handler is provided
- ExceptionHandlerExceptionReslover - Used when @ExceptionHandler is provided in the controller class or with @ControllerAdvice.
- ResponseStatusExceptionReslover- Used when @ResponseStatus annotation is used to map exceptions to HTTP status codes.
The overall flow in which resolver gets applied is below:
Conclusion
- Spring boot has a default exception handling mechanism if no other handler is provided.
- Spring boot converts all the exceptions to an internal server error.
- How exception handler in spring boot works.
- @ExceptionHandler provides flexibility to deal with the exception and return the response to the caller of API as per HTTP and REST standards.
Ready to streamline your Java projects? Enroll in our Free Java Spring Boot course and master the art of simplifying complex application development.