Exception Handling with the Spring 3.2 @ControllerAdvice Annotation

A short time ago, I wrote a blog outlining how I upgraded my Spring sample code to version 3.2 and demonstrating a few of the little ‘gotchas’ that arose. Since that I’ve been perusing Spring 3.2’s new feature list and whilst it doesn’t contain any revolutionary new changes, which I suspect the Guys at Spring are saving for version 4, it does contain a few neat upgrades. The first one that grabbed my attention was the new @ControllerAdvice annotation, which seems to neatly plug a gap in Spring 3 functionality. Let me explain…

If you take a look at my blog on Spring 3 MVC Exception Handlers you’ll see that the sample code contains a flaky controller with a request handler method that throws an
IOException. The IOException is then handled by another method in the same
 
controller that’s annotated with @ExceptionHandler(IOException.class). The problem is that your method that’s annotated with @ExceptionHandler(IOException.class) can only handle IOExceptions thrown by its containing controller. If you want to create a global exception handler that handles exceptions thrown by all controllers then you have to revert to something like Spring 2’s SimpleMapingExceptionHandler and some XMLconfiguration. Now things are different. To demonstrate the use of @ControllerAdvice I’ve created a simple Spring 3.2 MVC application that you can find on github. The application’s home page ostensively allows the user to display either their address or credit card details,

…except that when the user attempt to do this, the associated controllers will throw an
IOException and the application displays the following error page:

The controllers that generate the exceptions are fairly straightforward and listed below:

@Controller
public class UserCreditCardController {

  private static final Logger logger = LoggerFactory.getLogger(UserCreditCardController.class);

  /**
   * Whoops, throw an IOException
   */
  @RequestMapping(value = "userdetails", method = RequestMethod.GET)
  public String getCardDetails(Model model) throws IOException {

    logger.info("This will throw an IOException");

    boolean throwException = true;

    if (throwException) {
      throw new IOException("This is my IOException");
    }

    return "home";
  }

}
@Controller
public class UserAddressController {

  private static final Logger logger = LoggerFactory.getLogger(UserAddressController.class);

  /**
   * Whoops, throw an IOException
   */
  @RequestMapping(value = "useraddress", method = RequestMethod.GET)
  public String getUserAddress(Model model) throws IOException {

    logger.info("This will throw an IOException");

    boolean throwException = true;

    if (throwException) {
      throw new IOException("This is my IOException");
    }

    return "home";
  }

}

As you can see, all that this code does is to map userdetails and useraddress to the getCardDetails(...) and getUserAddress(...) methods respectively. When either of these methods throw an IOException, then the exception is caught by the following class:

@ControllerAdvice
public class MyControllerAdviceDemo {

  private static final Logger logger = LoggerFactory.getLogger(MyControllerAdviceDemo.class);

  @Autowired
  private UserDao userDao;

  /**
   * Catch IOException and redirect to a 'personal' page.
   */
  @ExceptionHandler(IOException.class)
  public ModelAndView handleIOException(IOException ex) {

    logger.info("handleIOException - Catching: " + ex.getClass().getSimpleName());
    return errorModelAndView(ex);
  }

  /**
   * Get the users details for the 'personal' page
   */
  private ModelAndView errorModelAndView(Exception ex) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("error");
    modelAndView.addObject("name", ex.getClass().getSimpleName());
    modelAndView.addObject("user", userDao.readUserName());

    return modelAndView;
  }
}

The class above is annotated by the new @ControllerAdvice annotation and contains a single public method handleIOException(IOException.class). This method catches all IOExceptions thrown by the controllers above, generates a model containing some relevant user information and then displays and error page. The nice thing about this is that,no matter how many controllers your application contains, when any of them throws an IOException, then it’ll be handled by the MyControllerAdviceDemo exception handler.

@ModelAttribute and @InitBinderOne final thing to remember is that the although the ControllerAdvice annotation is useful for handling exceptions, it can also be used the globally handle the @ModelAttribute and @InitBinder annotations. The combination of ControllerAdvice and @ModelAttribute gives you the facility to setup model objects for all controllers in one place and likewise the combination of ControllerAdvice and @InitBinder allows you to attach the same custom validator to all your controllers, again, in one place.
 

Reference: Exception Handling with the Spring 3.2 @ControllerAdvice Annotation from our JCG partner Roger Hughes at the Captain Debug’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


nine + = 11



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close