gpt4 book ai didi

spring - @ControllerAdvice 是否覆盖 SimpleMappingExceptionResolver?

转载 作者:行者123 更新时间:2023-12-05 01:04:32 26 4
gpt4 key购买 nike

我有 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 ?

最佳答案

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

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

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

如果您想同时使用这两种策略,您可以探索的一种可能解决方法是使用 HandlerExceptionResolverCompositethis 中所述博文

关于spring - @ControllerAdvice 是否覆盖 SimpleMappingExceptionResolver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com