Upgrading to spring-3.1 seems to break my CustomWebArgumentResolver

前端 未结 1 572
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 06:16

I\'m trying to upgrade a spring MVC app from 3.0.6 to 3.1.2 and some controllers that used to work don\'t seem to work anymore. I\'ve read the spring docs, but I\'m confuse

相关标签:
1条回答
  • 2020-12-17 06:43

    The API has changed with Spring 3.1 - the interface to implement to resolve a controller argument is HandlerMethodArgumentResolver. You can continue to use CustomWebArgumentResolver, by adapting it to a HandlerMethodArgumentResolver

    However changing the code to use HandlerMethodArgumentResolver also will be easy:

    public class CustomWebArgumentResolver implements HandlerMethodArgumentResolver {
        private AsOfDateConverter asOfDateConverter;
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
            if (isAsOfDateParameter(methodParameter)) {
                return asOfDateConverter.convert(webRequest.getParameter("asOf"));
            }
    
            return UNRESOLVED;
    
        }
    
    
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return (methodParameter.getParameterAnnotation(AsOf.class)!=null)
        }
    

    Edit

    After looking through your comments, I think I have an idea about what could be going wrong. Can you please check your @AsOf annotation, you probably have not declared the retention of Runtime, which could be the reason why the the WebArgumentResolver is not taking effect:

    @Retention(RetentionPolicy.RUNTIME)
    public @interface AsOf {
    
    }
    

    Anyway here is a gist with a full working test along the same lines:

    https://gist.github.com/3703430

    0 讨论(0)
提交回复
热议问题