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.
I do not think this is possible now. If you can make the API and View controllers throw different Exception types, then you could define two different @ExceptionHandlers and achieve what you want.
You could use aop to translate the Exceptions coming out of APIs to a standard APIException. See this thread on spring forums.
Hope it helps.