Wednesday, 12 July 2017

Diffrence between @Controller and @ControllerAdvice in Spring Boot

@Controller 

  • @Controller is used to mark classes as Spring MVC Controller (e.g. a web controller).
  • It is typically used in combination with annotated handler methods based on the RequestMapping annotation.

@ExceptionHandler

@ExceptionHandler works at the Controller level and it is only active for that particular Controller, not globally for the entire application.

HandlerExceptionResolver

This will resolve any exception thrown by the application. It is used to resolve standard Spring exceptions to their corresponding HTTP Status Codes. It does not have control over the body of the response, means it does not set anything to the body of the Response.It does map the status code on the response but the body is null.

@ControllerAdvice

@ControllerAdvice used for global error handling in the Spring MVC application.It also has full control over the body of the response and the status code.