I am trying to process a CSV file in which some of the fields are format dates "yyyy-MM-dd", but the reader crashes when it tries to convert a String from a CSV file to Date in my model class.
Error:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 error Field error in object 'target' in field 'datetimeInactive': value rejected [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 the value of a property of type "java.lang.String" to the required type 'java.util.Date' for the property 'datetimeInactive'; nested exception java.lang.IllegalStateException: cannot convert value of type [java.lang.String] for the required type [java.util.Date] for the 'datetimeInactive' property: no suitable editors or conversion strategy found]
XML for the reader:
http://code.google.com/p/springbatch-in-action/source/browse/trunk/sbia/ch07/src/test/resources/com/manning/sbia/ch07/test-batch-reader-context. xml? r = 145
In my XML configuration files, I have the following beans:
<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>
My questions:
UPDATE:
I have an answer to question number 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">
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</set>
</property>
</bean>
Custom converter:
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. I.e., , 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