I have two types of controllers in my spring application.
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.
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.
UPDATE
I am using spring 4. You can do one of 2 below options.
(1) You can add the packages you want. (Inside those packages you have controllers that you want to follow @ControllerAdvice
).
Ex:
@ControllerAdvice(basePackages={"my.pkg.a", "my.pkg.b"})
(2) You can directly add the controller classes you want.
Ex:
@ControllerAdvice(basePackageClasses={MyControllerA.class, MyControllerB.class})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With