Spring MVC Spring 4异常处理:没有适合参数的冲突解决程序

0h4hbjxa  于 2023-04-06  发布在  Spring
关注(0)|答案(3)|浏览(147)

问题陈述

Spring 3迁移到Spring 4会在异常处理流程中引发一些异常。该异常在org.springframework.web.method.support.InvocableHandlerMethod类中表示No suitable resolver for argument
因此,每当异常发生时,Spring都会尝试查找它所获取的异常处理程序,但当它尝试填充方法参数或异常处理程序时,它会抛出以下异常
调用@ExceptionHandler方法失败:

public org.springframework.web.servlet.ModelAndView  
       HelloController.handleCustomException(CustomGenericException, javax.servlet.http.HttpServletRequest, org.springframework.web.servlet.ModelAndView)

  java.lang.IllegalStateException: 
     No suitable resolver for argument [2] 
             [type=org.springframework.web.servlet.ModelAndView]

HandlerMethod详细信息:

Controller [HelloController]
Method [public org.springframework.web.servlet.ModelAndView  
        HelloController.handleCustomException(CustomGenericException,
            javax.servlet.http.HttpServletRequest,org.springframework.web.servlet.ModelAndView)]
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(
                     InvocableHandlerMethod.java:169)

它基本上是针对@CRequestParam("p") String p变量的

编码

控制器

@RequestMapping(method = RequestMethod.GET, value="/exception2")
    public String getException1(ModelMap model, @CRequestParam("p") String p) {

        System.out.println("Exception 2 "+ p);
        throw new CustomGenericException("1","2");
    }

异常处理程序

@ExceptionHandler(CustomGenericException.class)
    public ModelAndView handleCustomException(CustomGenericException ex, 
            HttpServletRequest request, @CRequestParam("p") String p) {

            ModelAndView model = new ModelAndView("error/generic_error");
            model.addObject("exception", ex);
            System.out.println("CustomGenericException  ");
            return model;
    }

注解

@Target( { ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CRequestParam {
    String value() default "";
}

参数解析器

public class CRequestparamResolver implements HandlerMethodArgumentResolver {

    @Override
        public boolean supportsParameter(MethodParameter methodParameter) {
          CRequestParam requestParamAnnotation = 
          methodParameter.getParameterAnnotation(CRequestParam.class);
        if(requestParamAnnotation==null){
        return false;
        }
        return true;
        }

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
        WebDataBinderFactory binderFactory) throws Exception {

    CRequestParam requestParamAnnotation = methodParameter .getParameterAnnotation(CRequestParam.class);

    if (requestParamAnnotation != null) {
        String requestParamName = requestParamAnnotation.value();
        if (StringUtils.hasText(requestParamName)) {
        return webRequest.getParameter(requestParamName);
        }
    }
    return null;
  }

XML配置

<bean
        class="com.mkyong.common.resolver.AnnotationMethodHandlerAdapterConfigurer"
        init-method="init">
        <property name="customArgumentResolvers">
            <list>
                <bean class="com.mkyong.common.resolver.CRequestparamResolver" />
            </list>
        </property>
    </bean>

    <bean 
 class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
        <property name="customArgumentResolvers">
            <list>
                <bean class="com.mkyong.common.resolver.CRequestparamResolver" />
            </list>
        </property>
    </bean>

源代码

https://github.com/santoshjoshi/SpringMVC4

uidvcgyl

uidvcgyl1#

通过在请求本身中传递自定义参数解决了问题。
代码如下:

控制器

@RequestMapping(method = RequestMethod.GET, value = "/exception2")
public String getException1(ModelMap model, @CRequestParam("p") String p, HttpServletRequest request) {

  System.out.println("Exception 2 " + p);
  request.setAttribute("p", p);
  throw new CustomGenericException("1", "2");
}

异常处理程序

@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex, HttpServletRequest request) {

  ModelAndView model2 = new ModelAndView("error/generic_error");
  model2.addObject("exception", ex);
  System.out.println(request.getAttribute("p"));
  System.out.println("CustomGenericException  ");
  return model2;

}

完整的源代码可在git获得

xtfmy6hx

xtfmy6hx2#

在我的例子中,我没有将@RequestBody添加到我的客户端接口,所以它不知道如何解析参数。

u0sqgete

u0sqgete3#

通过提供WebApplicationInitializer的实现解决了这个问题

public class SpringDispatcherConfig implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext Contianer =   new AnnotationConfigWebApplicationContext();
    Contianer.register(SpringConfig.class);
    Contianer.setServletContext(servletContext);
    DispatcherServlet dispatcherServlet = new DispatcherServlet(Contianer);
    dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
    Dynamic servlet = servletContext.addServlet("spring",dispatcherServlet);
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);

}

相关问题