Handling Exceptions with RestControllerAdvice, ExceptionHandler
2 min read
Java: 1.8 Spring Boot: 2.7.5
Overview
Exception handling is a very important part of developing applications. In this article, I will introduce how to use RestControllerAdvice and ExceptionHandler to handle exceptions.
Basic exception handling in Spring
By default, when an exception occurs, error handling depends on the environment you accessed.
If you accessed a web page, you will receive the Whitelabel error page.

SampleController

Exception
Spring Boot has a BasicErrorController implemented for error handling, and by default Spring Boot has a WAS setup that forwards error requests back to /error when an exception occurs.
Request flow
WAS -> Filter -> DispatcherServlet -> Interceptor -> Controller
If an exception occurs in the Controller (Service, DB…),
it will call /error once more.
WAS -> Filter -> DispatcherServlet -> Interceptor -> Controller
-> (throw Exception) -> Interceptor -> DispatcherServlet -> Filter
-> WAS -> Filter -> DispatcherServlet -> Interceptor -> Controller(/error)
In a REST API, you will receive the following message, which is not useful from the client’s point of view.

Exception Body
To solve this, let’s implement it so that it returns an exception (message, errorCode) that we created ourselves.
There are many ways, but I only describe ExceptionHandler and RestControllerAdvice.
ExceptionHandler
Where it can be used: in
Controller,RestControllerAdvice
Exceptions thrown are handled by ExceptionHandler through ExceptionHandlerExceptionResolver.
You can use this to easily handle errors that occur in the Controller.

SampleController

Exception Result
RestControllerAdvice
RestControllerAdvice is registered as a Spring bean, and ExceptionHandler can be applied globally.

ServiceExceptionHandler

ControllerAdvice Annotation

RestControllerAdvice Annotation
Exception handling flow

Exception handling flow
- ExceptionHandlerExceptionResolver works
- Check whether there is an appropriate
ExceptionHandlerin the controller that threw the exception - If there is no suitable
ExceptionHandler, go toControllerAdvice - Check whether there is a suitable
ExceptionHandlerinControllerAdvice, and if not, go to the next handler
- Check whether there is an appropriate
- ResponseStatusExceptionResolver works
- Check whether ResponseStatus exists or whether it is a ResponseStatusException
- If so, the exception is passed to the servlet by
sendError()in the ServletResponse, and the servlet passes the request to the BasicErrorController.
- DefaultHandlerExceptionResolver works
- Check whether it is an internal Spring exception; if so, handle the error, otherwise skip
- Because there is no suitable ExceptionResolver, the exception is passed to the servlet, which passes the request back to the BasicErrorController according to the auto-configuration done by Spring Boot.
Implementing ExceptionHandler
As a simple example, let’s implement returning our own exception when a post can’t be found by post id
Create a list of exceptions using an enum class.

PostErrorResult
Create an Exception that extends RuntimeException.

PostException
Finally, create a RestControllerAdvice.

ServiceExceptionHandler
Result

PostService

Response Body (PostException)
Implement and use an appropriate exception form for your service.