Home  >  Spring MVC

Spring MVC @ControllerAdvice Annotation Example

By Arvind Rai, April 18, 2015
@ControllerAdvice annotation is a specialization of @Component. The classes annotated with @ControllerAdvice are auto detected by classpath scanning. The use of @ControllerAdvice is advising all or selected controllers for @ExceptionHandler, @InitBinder and @ModelAttribute. What we have to do is create a class annotated with @ControllerAdvice and create required method which will be annotated with @ExceptionHandler for global exception handling, @InitBinder for global init binding and @ModelAttribute for global model attributes addition. Whenever a request comes to controller and its method with @RequestMapping and if there is no locally defined @ExceptionHandler, @InitBinder and @ModelAttribute, the globally defined class annotated with @ControllerAdvice is served.
@ControllerAdvice has attributes annotations, assignableTypes and basePackages . To use @ControllerAdvice, we should use @EnableWebMvc annotation in our java config.

To understand more about @InitBinder and @ExceptionHandler, visit our another post.
1. Spring MVC Validator with @InitBinder
2. Spring MVC Exception Handling with @ExceptionHandler

Create a class Annotated with @ControllerAdvice

Find the sample class annotated with @ControllerAdvice. Here we are declaring methods for @ExceptionHandler, @InitBinder and @ModelAttribute to understand how to use it. We are using basePackages attribute of @ControllerAdvice to limit the controller classes. In this attribute we can define the array of base packages of controllers which can be used by @ControllerAdvice class, if not used basePackages attribute, then all controllers in classpath annotated with @Controller will be served by our @ControllerAdvice class.
@ControllerAdvice(basePackages = {"com.concretepage.controller"} )
public class GlobalControllerAdvice {
	public void dataBinding(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
        public void globalAttributes(Model model) {
		model.addAttribute("msg", "Welcome to My World!");
        public ModelAndView myError(Exception exception) {
	    ModelAndView mav = new ModelAndView();
	    mav.addObject("exception", exception);
	    return mav;

Create Controller

Find the controller with a @RequestMapping method for the demo. Here we have also defined a controller specific @InitBinder method.
public class MyWorldController {
	private UserValidator userValidator;
	@RequestMapping(value="signup", method = RequestMethod.GET)
	public ModelAndView user(){
		return new ModelAndView("user","user",new User());
	public void dataBinding(WebDataBinder binder) {
	@RequestMapping(value="save", method = RequestMethod.POST)
	public String createUser(@ModelAttribute("user") @Valid User user,BindingResult result, ModelMap model) 
			                                                throws FileNotFoundException {
	    if(result.hasErrors()) {
	    	return "user";
	    if(user.getName().equals("exception")) {
	        throw new FileNotFoundException("Error found.");	
	    System.out.println("Name:"+ user.getName());
	    System.out.println("Date of Birth:"+ user.getDob());
	    return "success";
The exception FileNotFoundException thrown in the controller is not being handled, so it will be handled by global exception handling class that is annotated by @ControllerAdvice. For the date validation, we have not local WebDataBinder configuration, it will be handled by global @InitBinder method. @ModelAttribute for "msg" key is not added in the controller locally. So the method annotated by @ModelAttribute in the GlobalControllerAdvice class is used.

Download Complete Source Code


©2019 concretepage.com | Privacy Policy | Contact Us