Spring @ControllerAdvice Annotation Example
In this section we will learn about @ControllerAdvice Annotation. The @ControllerAdvice is a specialization of the @Component annotation which sanctions to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and kindred. It declares @ExceptionHandler , @InitBinder , or @ModelAttribute methods to be shared across multiple @Controller classes. ResponseEntityExceptionHandler is a convenient base class for @ControllerAdvice classes that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods. It provides an methods for handling internal Spring MVC exceptions. It returns a ResponseEntity in contrast to DefaultHandlerExceptionResolver which returns a ModelAndView . The following example creates a Spring Boot web application which uses @ControllerAdvice . Proj...