Exception Handling II

Learn how a global exception handler works.

PlayerErrorResponse is an error response class containing information that we want to send to the client. PlayerNotFoundException is a custom exception class. 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.