Exception Handling II
Learn how a global exception handler works.
We'll cover the following
In the last lesson, we created an error response class, PlayerErrorResponse
, containing information that we want to send to the client. We also created a custom exception class, PlayerNotFoundException
. The REST service throws exceptions of this class when a player record is not found in the database.
@ControllerAdvice
A best practice in exception handling is to have centralized exception handlers that can be used by all controllers in the REST API. Since exception handling is a cross-cutting concern, Spring provides the @ControllerAdvice
annotation. This annotation intercepts requests going to the controller and responses coming from controllers.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.