Java Spring 3.1+;参数解析程序无法处理私有Person类参数
我刚从Spring3.0升级到3.2。我在spring配置xml文件中配置了如下自定义参数解析器:Java Spring 3.1+;参数解析程序无法处理私有Person类参数,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我刚从Spring3.0升级到3.2。我在spring配置xml文件中配置了如下自定义参数解析器: <mvc:annotation-driven> <mvc:argument-resolvers> <bean class="mycompany.api.PersonResolver" /> </mvc:argument-resolvers> </mvc:annotation-driven> 其中Person类是我们
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="mycompany.api.PersonResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
其中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反射,并且与某个Spring Person类(Spring security有一个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;
}
}
弹簧配置:
<?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>
它不应该引起任何名称冲突问题,因为软件包就是这样发明的。您能否向我们展示PersonResolver
实现?您能否发布DispatcherServlet
的完整配置。请在PersonResolver
初始化代码中添加日志语句,以帮助确定它是否实际加载。如果您的示例是准确的,那么XML Beans文件中的包名与实际类不匹配。它在启动时被调用。当我使用我的变通包装类时,它也调用supportsParameter方法,但对于我的Person参数,它甚至不调用此方法……我现在发现我们的Person类实现了映射接口,这导致了问题。当我试图解决实现映射接口的参数时,它失败了!显然,SpringMVC试图在我们自己的解析器得到机会之前解决它。
<?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>