Upgrading to spring-3.1 seems to break my CustomWebArgumentResolver

↘锁芯ラ 提交于 2020-01-10 02:18:08

问题


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 confused about what's compatible with what.

We've got a CustomWebArgumentResolver that looks for any request parameter named "asOf" and coverts its value to a date. We call it, unimaginatively, the "AsOfDateConverter." When upgrading to spring-3.1.2, I took advantage of the new namespace functionality and added this to my applicationContext:

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:argument-resolvers>
        <bean id="customWebArgumentResolver" class="my.converters.CustomWebArgumentResolver">
        </bean>            
    </mvc:argument-resolvers>
</mvc:annotation-driven>

The CustomWebArgumentResolver is straightforward:

public class CustomWebArgumentResolver implements WebArgumentResolver {
    private AsOfDateConverter asOfDateConverter;

    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
        if (isAsOfDateParameter(methodParameter)) {
            return asOfDateConverter.convert(webRequest.getParameter("asOf"));
        }

        return UNRESOLVED;
    }

Then an example controller might look something like this:

@Controller
@Secured({BaseController.ROLE_LOGGED_IN})
@org.springframework.transaction.annotation.Transactional
public class DashboardController extends BaseController {
    public static final String URL = "/dashboard";

    @RequestMapping(value=URL, method=RequestMethod.GET)
    public ModelAndView get(@RequestParam(required=false) String requestedMeterType, @AsOf Date asOf) {
        debug(log, "Rendering dashboard asOf %s", asOf);
etc etc

The "asOf" parameter is coming in null, and I'm sure I'm missing something obvious. If anyone out there neck deep in the latest MVC 3.1 stuff could point me in the right direction I'd be grateful.

Thanks! Tom

EDIT: The AsOf annotation:

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

More of my applicationContext:

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:argument-resolvers>
        <bean class="[blah].AsOfDateHandlerMethodArgumentResolver">
            <property name="asOfDateConverter">
                <bean class="[blah].AsOfDateConverter"/>
            </property>
        </bean> 
    </mvc:argument-resolvers>
</mvc:annotation-driven>

<!-- Added to re-support @Controller annotation scanning after upgrading to spring-3.1. -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="[blah].converters.CustomerConverter"/>
            <bean class="[blah].converters.AccountConverter"/>
            <bean class="[blah].converters.DateConverter"/>
            <bean class="[blah].converters.CustomerCommunicationInstanceConverter"/>
            <bean class="[blah].converters.MeterTypeConverter"/>
            <bean class="[blah].converters.AreaAmountConverter" p:precision="0"/>
            <bean class="[blah].converters.LengthAmountConverter" p:precision="1"/>
        </set>
    </property>
</bean>

回答1:


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



来源:https://stackoverflow.com/questions/12357817/upgrading-to-spring-3-1-seems-to-break-my-customwebargumentresolver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!