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.
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
- Spring Integration - Inbound file endpoint. How to
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- spring-mvc a标签带参怎样向controller发送请求。路径格式?
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
(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
HandlerExceptionResolver
where 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@ExceptionHandler
methods 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
@ExceptionHandler
works. In Spring 3.1 there is a classExceptionHandlerExceptionResolver
. This class implements (with help of its superclasses) the interfaceHandlerExceptionResolver
and is responsible invoking the@ExceptionHandler
methods.The
HandlerExceptionResolver
interface 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 thehandler
parameter.The
ExceptionHandlerExceptionResolver
uses 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. (elsenull
get returned in order to signal that this exception resolver feels no responsible).The first idea would be to implement an own
HandlerExceptionResolver
that behaves likeExceptionHandlerExceptionResolver
, but instead of search for@ExceptionHandler
in 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 onlyExceptionHandlerExceptionResolver
is done by spring automatically). So I came up with another idea:Implement a simple
HandlerExceptionResolver
that "forwards" the exception to THE (already configured)ExceptionHandlerExceptionResolver
, BUT with an modifiedhandler
which 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:
GlobalMethodHandlerExeptionResolver
The global Handler has to implement this interface (in order to get found and to implement the
fakeHanderMethod
used for thehandler
And example for an global Handler:
BTW: You do not need to register the
GlobalMethodHandlerExeptionResolver
because spring automatically register all beans that implementsHandlerExceptionResolver
for exception resolvers. So a simple<bean class="GlobalMethodHandlerExeptionResolver"/>
is enough.An abstract class where you define the exception handlers will do. And then make your controllers inherit it.
Since Spring 3.2 you can use @ControllerAdvice annotation. You can declare an @ExceptionHandler method within an @ControllerAdvice class in which case it handles exceptions from @RequestMapping methods from all controllers.