我刚从Spring 3.0升级到3.2。我在我的spring配置xml文件中配置了这样的自定义参数解析器:
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="mycompany.api.PersonResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
我的Spring MVC控制器有一个这样的方法:
@RequestMapping(value="/api/doSomething", method=RequestMethod.POST)
public @ResponseBody JsonNode handleRestCall(Person person) {
...
return jsonNode;
}
Person类是我们自己的专有类。
然而,这不起作用。它给了我一个错误:
Rest call error argument type mismatch
HandlerMethod details:
Controller [MyController]
...
Resolved arguments:
[0] [type=org.springframework.validation.support.BindingAwareModelMap] [value= {org.springframework.validation.BindingResult.objectNode=org.springframework.validation.BeanPropertyBindingResult: 0 errors}]
java.lang.IllegalArgumentException: argument type mismatch
似乎将我的Person参数类型更改为其他类型(在我的情况下:PersonWrapper,包含对Person的引用)为我提供了该问题的解决方法,即Spring现在可以解决该参数。
有人可以向我解释一下吗?这是因为Spring使用java Reflection并且与一些Spring Person类有一些名称冲突(Spring安全有一个org.springframework.security.ldap.userdetails.Person类)?
我的解析器的实施: 我最初的实现看起来像:
import mycompany.Person;
public class PersonResolver implements HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter mp) {
return mp.getParameterType().equals(Person.class);
}
public Object resolveArgument(MethodParameter mp, ModelAndViewContainer mavc, NativeWebRequest nwr, WebDataBinderFactory wdbf) throws Exception {
if(mp.getParameterType().equals(Person.class)) {
...
return somePerson;
}
return UNRESOLVED;
}
}
Spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config />
<!-- automatically search for Controller classes using @Controller annotation -->
<context:component-scan base-package="mycompany.api"/>
<!-- we use Spring 3 annotation driven development -->
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="mycompany.api.PersonResolver" />
<bean class="mycompany.api.SessionLocaleResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
</beans>