@ControllerAdvice是否覆盖SimpleMappingExceptionResolver?

时间:2014-04-09 21:50:59

标签: spring spring-mvc exception-handling

我已SimpleMappingExceptionResolver配置如此。

@Bean(name = "simpleMappingExceptionResolver")
public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
    SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver();

    Properties mappings = new Properties();
    mappings.setProperty("InvalidRequestException", "error");
    mappings.setProperty("GenericServerException", "error");
    mappings.setProperty("IllegalArgumentException", "error");

    r.setExceptionMappings(mappings);
    r.setDefaultErrorView("error");
    r.setExceptionAttribute(DEFAULT_EXCEPTION_ATTRIBUTE);
    r.setWarnLogCategory("org.springframework.web.servlet.handler.SimpleMappingExceptionResolver");
    return r;
}

但是我为@ControllerAdvice定义了MethodArgumentNotValidException.class并置于@ComponentScan范围内。但由于某些原因,@ExceptionHandler类中的@ControllerAdvice注释方法未被调用。但我确实看到了堆栈跟踪。它促使我思考,spring正在为MethodArgumentNotValidException.class使用一些defaultHandler。

WARN : org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Handler execution resulted in exception
org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: public com.mrll.global.profile.model.PasswordInfo com.mrll.global.controller.PasswordController.passwordInfo(com.mrll.global.core.password.PasswordChange) throws com.mrll.global.profile.PasswordChangeException, with 1 error(s): [Field error in object 'passwordChange' on field 'oldPassword': rejected value []; codes [NotEmpty.passwordChange.oldPassword,NotEmpty.oldPassword,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [passwordChange.oldPassword,oldPassword]; arguments []; default message [oldPassword]]; default message [may not be empty]] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.validate(RequestResponseBodyMethodProcessor.java:119)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:101)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
    at java.lang.Thread.run(Thread.java:662)
DEBUG: com.mrll.utility.Constants - end ConstantsReloader

为什么不调用ControllerAdvice?

1 个答案:

答案 0 :(得分:5)

正如this博客文章所解释的,Spring只能加载一个HandlerExceptionResolver

这意味着当你将它的实现指定为bean(这正是你用SimpleMappingExceptionResolver做的那样)时,Spring将不会使用它的默认实现(直到Spring之前) 3.2是AnnotationMethodHandlerExceptionResolver - 从Spring 3.2开始,Spring使用的默认实现是ExceptionHandlerExceptionResolver)。

Spring使用的默认实现是使用@ExceptionHandler来处理异常的实现,这就是该方法开箱即用的原因。

如果您想要同时使用这两种策略,可以尝试探索的方法是使用this博文

中所述的HandlerExceptionResolverComposite