我正在尝试处理CSV文件,其中某些字段是 "yyyy-MM-dd"
格式的日期 - 但是当尝试将CSV文件中的字符串转换为模型类中的日期时,读取器失败 .
错误是:
org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1 error字段'datetimeInactive'中对象'target'中的字段错误:被拒绝的值[2011-04-27];代码[typeMismatch.target.datetimeInactive,typeMismatch.datetimeInactive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [target.datetimeInactive,datetimeInactive];参数[];默认消息[datetimeInactive]];默认消息[无法将类型'java.lang.String'的属性值转换为属性'datetimeInactive'的必需类型'java.util.Date';嵌套异常是java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为属性'datetimeInactive'所需的类型[java.util.Date]:找不到匹配的编辑器或转换策略]
读者的XML:
在我的XML配置文件中,我有以下bean:
<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
<constructor-arg>
<bean class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
</constructor-arg>
<constructor-arg value="true" />
</bean>
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<ref local="dateEditor" />
</entry>
</map>
</property>
</bean>
我的问题是:
-
我在我的上下文中定义了
CustomDateEditor
- 为什么Spring不能将String转换为Date? -
我已经读过在Spring 3(
Converter
?)中有一种更新的方式来完成转换 . 即http://forum.springsource.org/showthread.php?108480-Register-TypeConverter-PropertyEditor-w-Spring-Batch - 但是,我在Spring Batch文档中找不到任何示例代码 . 你能在这里展示一下怎么做/指出一些链接吗?
UPDATE:
我得到了问题#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;
}
}
我仍在寻找问题#1的答案 . 即,在设置转换器之后,我仍然在批处理任务期间获得BindException . 从this forum thread开始,看起来我的代码应该执行了转换 .
堆栈跟踪是:
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
1 回答
您的论坛参考用于构建应用程序上下文和配置bean时的类型转换
看看_D55872_的JavaDoc
这意味着您应该将CustomDateEditor直接注入Mapper