How to configure which controllers Spring @ControllerAdvice will be applied to?

ams picture ams · Jan 27, 2013 · Viewed 10.8k times · Source

I have two types of controllers in my spring application.

  • View controllers that forward to views to generate HTML
  • API controllers that return JSON directly from the controllers

Both the API and View controllers are part of the same spring dispatcher servlet. Spring 3.2 introduced the @ControllerAdvice annotation to allow for a global location to handle exception.

The documentation implies that @ControllerAdvice will be applied to every controller associated with a Dispatcher Servlet.

Is there a way to configure which controllers @ControllerAdvice will apply to?

For example in my scenario I want a @ControllerAdvice for my View Controllers and separate @ControllerAdvice for my API controllers.

Answer

Adam from WALCZAK.IT picture Adam from WALCZAK.IT · Feb 14, 2014

For people that will still find this question:

As from Spring 4 ControlerAdvice's can be limited to Controler's with the specified annotations. Take a look at:

http://blog.codeleak.pl/2013/11/controlleradvice-improvements-in-spring.html

(second half of this article) for more details.