Spring Webflow 绑定:转换器 - java.lang.IllegalArgumentException:每个转换器对象必须实现 Converter ... 接口之一
我在 Spring 的 XML 配置文件之一中有以下代码:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.springframework.binding.convert.converters.StringToDate">
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</list>
</property>
</bean>
但是在部署期间(在 JBoss 上)出现以下异常:
java.lang.IllegalArgumentException:每个转换器对象必须 实现 Converter、ConverterFactory 或 GenericConverter 之一 接口
知道为什么吗?据我所知,org.springframework.binding.convert.converters.StringToDate 是一个实现转换器
。
更新:
刚刚找到这个答案,这表明混合使用 Converter
和 PropertyEditor
可能会导致问题。我的应用程序中确实有部分使用PropertyEditor,但据我所知,文档并未讨论混合两个系统的任何问题。
堆栈跟踪:
Caused by: java.lang.IllegalArgumentException: Each converter object must implement one of the Converter, ConverterFactory, or GenericConverter interfaces
at org.springframework.core.convert.support.ConversionServiceFactory.registerConverters(ConversionServiceFactory.java:106)
at org.springframework.context.support.ConversionServiceFactoryBean.afterPropertiesSet(ConversionServiceFactoryBean.java:56)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 146 more
更新 2:
我将 xml 更改为:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="my.project.StringToDate">
<!-- org.springframework.binding.convert.converters.StringToDate DEFAULT_PATTERN = "yyyy-MM-dd" -->
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</set>
</property>
</bean>
我的自定义转换器是:
package my.project;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class StringToDate extends org.springframework.binding.convert.converters.StringToDate implements Converter<String, Date> {
public Date convert(String source) {
Date date = null;
try {
date = (Date) convertSourceToTargetClass(getPattern(), getTargetClass());
} catch (Exception e) {
}
return date;
}
}
但是,读取 以下内容论坛主题 我期望转换能够发挥作用。如果我理解正确,他们会说,一旦转换器设置正确,它就应该与 Spring Batch 一起工作,即不需要任何特殊设置即可使其专门与 Spring Batch 一起工作。但我在批处理任务期间仍然收到 BindException...知道为什么吗?
查看新的堆栈跟踪:
Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'target' on field 'datetimeInactive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeInactive,typeMismatch.datetimeInactive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeInactive,datetimeInactive]; arguments []; default message [datetimeInactive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeInactive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeInactive': no matching editors or conversion strategy found]
Field error in object 'target' on field 'datetimeActive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeActive,typeMismatch.datetimeActive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeActive,datetimeActive]; arguments []; default message [datetimeActive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeActive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeActive': no matching editors or conversion strategy found]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:186)
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:42)
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:179)
... 45 more
另请参阅我的原始问题< /a> (仍未解决)。
I have the following code in one of the Spring's XML config files:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.springframework.binding.convert.converters.StringToDate">
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</list>
</property>
</bean>
But I am getting the following exception during deployment (on JBoss):
java.lang.IllegalArgumentException: Each converter object must
implement one of the Converter, ConverterFactory, or GenericConverter
interfaces
Any idea why? As far as I can see, org.springframework.binding.convert.converters.StringToDate is an implementation of Converter
.
UPDATE:
Just found this answer, that suggests that mixing Converter
s and PropertyEditor
s might cause problems. I do have part in my app that use PropertyEditor
s, but as far as I can see, the documentation does not talk about any problem with mixing the two systems.
Stack trace:
Caused by: java.lang.IllegalArgumentException: Each converter object must implement one of the Converter, ConverterFactory, or GenericConverter interfaces
at org.springframework.core.convert.support.ConversionServiceFactory.registerConverters(ConversionServiceFactory.java:106)
at org.springframework.context.support.ConversionServiceFactoryBean.afterPropertiesSet(ConversionServiceFactoryBean.java:56)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 146 more
UPDATE 2:
I changed my xml to be:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="my.project.StringToDate">
<!-- org.springframework.binding.convert.converters.StringToDate DEFAULT_PATTERN = "yyyy-MM-dd" -->
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</set>
</property>
</bean>
My custom converter is:
package my.project;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class StringToDate extends org.springframework.binding.convert.converters.StringToDate implements Converter<String, Date> {
public Date convert(String source) {
Date date = null;
try {
date = (Date) convertSourceToTargetClass(getPattern(), getTargetClass());
} catch (Exception e) {
}
return date;
}
}
HOWEVER, reading the following forum thread I would expect the conversion to work. If I get them right, they are saying that once the converter is set correctly, it should be working with Spring Batch, i.e. it does not require any special setting to make it work specifically with Spring Batch. But I am still getting a BindException during the batch task... any idea why?
See the new stack trace:
Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'target' on field 'datetimeInactive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeInactive,typeMismatch.datetimeInactive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeInactive,datetimeInactive]; arguments []; default message [datetimeInactive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeInactive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeInactive': no matching editors or conversion strategy found]
Field error in object 'target' on field 'datetimeActive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeActive,typeMismatch.datetimeActive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeActive,datetimeActive]; arguments []; default message [datetimeActive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeActive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeActive': no matching editors or conversion strategy found]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:186)
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:42)
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:179)
... 45 more
See also my original question (still unsolved).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定应该使用 org.springframework.context.support.ConversionServiceFactoryBean 吗?这是一个 spring-core 定义的类。它期望以下内容:
您正在尝试向它发送一个
是 spring-core,另一个是 spring-webflow 转换器。您可能想尝试自己创建一个@Service
Are you sure you should be using the
org.springframework.context.support.ConversionServiceFactoryBean
? This is a spring-core defined class. It expects the following:You are trying to send it a
One is a spring-core, the other is a spring-webflow converter. You may want to try and create a @Service yourself