从Spring 3.0升级到Spring 4.0,破坏ExceptionHandlerExceptionResolver中的customArgumentResolvers

时间:2014-07-14 06:52:32

标签: java spring spring-mvc model-view-controller spring-3

我们已经将系统从Spring 3.0升级到了Spring 3.2,大多数情况都运行良好但是最近我们遇到了一个非常具有挑战性的问题,那就是在Bean中定义了CustomWebArgumentResolver属性并且我已经调试过代码,发现控件永远不会进入CPathResolver

中定义的方法resolveArgument和supportsParameter

以下是在配置文件webmvc-config.xml中创建的bean:

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

和CPathResolver.java

    public class CPathResolver implements HandlerMethodArgumentResolver {

        @Override
            public boolean supportsParameter(MethodParameter methodParameter) {
            CPathVariable pathParamAnnotation = methodParameter.getParameterAnnotation(CPathVariable.class);
            if(pathParamAnnotation==null) return false;

            return true;
            }

        @Override
            public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer,
                    NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
                    throws Exception {
            CPathVariable pathParamAnnotation = methodParameter.getParameterAnnotation(CPathVariable.class);
            if(pathParamAnnotation !=null) {
                String pathValue = pathParamAnnotation.value();
                if(StringUtils.hasText(pathValue)){
                    if(pathValue.equals("pos"))return "site";
                }
            }

            return null;
            }

    }

CPathVariable定义为:

    @Target( { ElementType.PARAMETER })

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    public @interface CPathVariable {

        String value() default "";

    }

在名为HTLReviewController的控制器中,我正在throw exception try catch block方法@RequestMapping中尝试@ExceptionHanler并在同一个控制器中通过 @Controller class HTLReviewController { @RequestMapping(value = "/review", method = RequestMethod.GET) public String review(Model model, @PathVariable(POS_VARIABLE) String pos, String searchCountryCode, String searchCityCode)throws MTException { /* some code */ try{ } catch(Exception e){ new MTException(0, "Error while getting details for abc : " + selectedHotelId, e); } @ExceptionHandler(MTException.class) public ModelAndView handleMTException(MTException ex,HttpServletRequest request,@CPathVariable(POS_VARIABLE) String pos ) { /* some code */ } } 注释处理异常。

I am getting below exception

org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView com.mmt.hotels.web.controllers.HTLReviewController.handleMTException(com.test.web.exceptions.MTException ,javax.servlet.http.HttpServletRequest,java.lang.String)|java.lang.IllegalStateException: No suitable resolver for argument [2] [type=java.lang.String]|

{{1}}

请大家向我提出解决此问题的宝贵建议。

0 个答案:

没有答案