最近在搭新项目的开发框架的时候,有些配置中需要使用到enum类,在以前项目的spring-mvc.xml中都是直接字符配置好就OK了,但是在新的项目中,启动的时候就是抛出org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.lang.String] to required type [com.kongming.api.interceptor.AnnotationHandlerInterceptor$AnnotationTarget] for property 'annotationTarget'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.kongming.api.interceptor.AnnotationHandlerInterceptor$AnnotationTarget] for property 'annotationTarget': no matching editors or conversion strategy found
后来我在对比新老项目的配置的时候发现在老项目中有一处是配置了org.springframework.format.support.FormattingConversionServiceFactoryBean.把这个配置加上去之前就正常了.(配置如下)
AnnotationTarget
enum AnnotationTarget {
<span style="white-space:pre"> </span>TYPE_AND_METHOD, TYPE, METHOD
}
AnnotationHandlerInterceptor
public class AnnotationHandlerInterceptor extends HandlerInterceptorAdapter {
private AnnotationTarget annotationTarget;
public void setAnnotationTarget(AnnotationTarget annotationTarget) {
this.annotationTarget = annotationTarget;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//TODO
}
}
spring-mvc.xml
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.kongming.spring.converter.DecryptionConversionServiceFactoryBean" />
</set>
</property>
</bean>
<mvc:interceptor>
<span style="white-space:pre"> </span><mvc:mapping path="/**" />
<bean class="com.kongming.api.interceptor.AnnotationHandlerInterceptor">
<property name="annotationTarget">
<value>TYPE_AND_METHOD</value>
</property>
</bean>
</mvc:interceptor>
具体什么原因还没有去搞清楚,目前也没有时间去看源码.以后弄清楚了再发一篇文章吧.如果有了解的同学,欢迎在评论中留言!