I want to create an exception handler which will intercept all controllers in my project. Is that possible to do? Looks like I have to put a handler method in each controller. Thanks for your help. I have a spring controller that sends Json response. So if an exception happens I want to send an error response which can be controlled from one place.
Share
(I found a way to implement it in Spring 3.1, this is described in the second part of this answer)
See chapter 16.11 Handling exceptions of Spring Reference
There are some more ways than using
@ExceptionHandler(see gouki’s answer)If you do not have a specific logic for the exception, but only specifc view then you could use the SimpleMappingExceptionResolver, which is at least an implementation of the
HandlerExceptionResolverwhere you can specify an Exception name pattern and the view (jsp) which is shown when the exception is thrown. For example:In Spring 3.2+ one can annotate a class with
@ControllerAdvice, all@ExceptionHandlermethods in this class work in a global way.In Spring 3.1 there is no
@ControllerAdvice. But with a little hack one could have a similar feature.The key is the understanding of the way
@ExceptionHandlerworks. In Spring 3.1 there is a classExceptionHandlerExceptionResolver. This class implements (with help of its superclasses) the interfaceHandlerExceptionResolverand is responsible invoking the@ExceptionHandlermethods.The
HandlerExceptionResolverinterface has only one Method:When the request was handled by a Spring 3.x Controller Method, then this method (represented by
org.springframework.web.method.HandlerMethod) is thehandlerparameter.The
ExceptionHandlerExceptionResolveruses thehandler(HandlerMethod) to obtain the Controller class and scan it for methods annotated with@ExceptionHandler. If one of this methods matches the exception (ex) then this methods get invoked in order to handle the exception. (elsenullget returned in order to signal that this exception resolver feels no responsible).The first idea would be to implement an own
HandlerExceptionResolverthat behaves likeExceptionHandlerExceptionResolver, but instead of search for@ExceptionHandlerin the controller class, it should search for them in one special bean. The drawback would be, that one has to (copy (or subclassExceptionHandlerExceptionResolver) and must) configure all nice message converters, argument resolvers and return value handlers by hand (the configuration of the real one and onlyExceptionHandlerExceptionResolveris done by spring automatically). So I came up with another idea:Implement a simple
HandlerExceptionResolverthat “forwards” the exception to THE (already configured)ExceptionHandlerExceptionResolver, BUT with an modifiedhandlerwhich points to the bean that contains the global Exception handlers (I call them global, because they do the work for all controllers).And this is the implementation:
GlobalMethodHandlerExeptionResolverThe global Handler has to implement this interface (in order to get found and to implement the
fakeHanderMethodused for thehandlerAnd example for an global Handler:
BTW: You do not need to register the
GlobalMethodHandlerExeptionResolverbecause spring automatically register all beans that implementsHandlerExceptionResolverfor exception resolvers. So a simple<bean class="GlobalMethodHandlerExeptionResolver"/>is enough.