Read following Spring batch job, it reads data from “domain.csv“, and map it to a domain object.

job-example.xml
<bean class="org.springframework.batch.item.file.FlatFileItemReader" >
 
  <property name="resource" value="file:outputs/csv/domain.csv" />
  <property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
 
	<property name="lineTokenizer">
	  <bean
		class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
		<property name="names" value="id, domainName, lastModifiedDate" />
	  </bean>
	</property>
	<property name="fieldSetMapper">
	  <bean
		class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
		<property name="prototypeBeanName" value="domain" />
	  </bean>
	</property>
 
    </bean>
  </property>
 
</bean>
 
 <bean id="domain" class="com.mkyong.batch.Domain" scope="prototype" />
domain.csv
1,facebook.com,Mon Jul 15 16:32:21 MYT 2013
2,google.com,Mon Jul 15 16:32:21 MYT 2013
3,youtube.com,Mon Jul 15 16:32:21 MYT 2013
4,yahoo.com,Mon Jul 15 16:32:21 MYT 2013
5,amazon.com,Mon Jul 15 16:32:21 MYT 2013
Domain.java
import java.util.Date;
 
public class DomainRanking {
 
	private int id;
	private String domainName;
	private Date lastModifiedDate;
 
	//...
}

Problem

The problem is how to map/convert the String Date Mon Jul 15 16:32:21 MYT 2013 to java.util.Date? Running above job will prompts following error messages :

Cannot convert value of type [java.lang.String] to required type [java.util.Date] 
        for property 'lastModifiedDate': 
	no matching editors or conversion strategy found

Solution

Refer to the BeanWrapperFieldSetMapper JavaDoc :

To customize the way that FieldSet values are converted to the desired type for injecting into the prototype there are several choices. You can inject PropertyEditor instances directly through the customEditors property…

To fix it, declares a CustomDateEditor and inject into BeanWrapperFieldSetMapper via customEditors property.

job-example.xml
<bean id="dateEditor" 
  class="org.springframework.beans.propertyeditors.CustomDateEditor">
  <constructor-arg>
	<bean class="java.text.SimpleDateFormat">
              <constructor-arg value="EEE MMM dd HH:mm:ss z yyyy" />
	</bean>
  </constructor-arg>
  <constructor-arg value="true" /> 
</bean>
 
<bean class="org.springframework.batch.item.file.FlatFileItemReader" >
 
  <property name="resource" value="file:outputs/csv/domain.csv" />
  <property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
	<property name="lineTokenizer">
	  <bean
		class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
		<property name="names" value="id, domainName, lastModifiedDate" />
	  </bean>
	</property>
	<property name="fieldSetMapper">
	  <bean
		class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
		<property name="prototypeBeanName" value="domain" />
		<property name="customEditors">
		  <map>
			<entry key="java.util.Date">
			     <ref local="dateEditor" />
			</entry>
		  </map>
		</property>
	  </bean>
	</property>
 
    </bean>
  </property>
</bean>
 
<bean id="domain" class="com.mkyong.batch.Domain" scope="prototype" />

P.S The String Date “Mon Jul 15 16:32:21 MYT 2013″ is represented by “EEE MMM dd HH:mm:ss z yyyy”.

Note
Do not injects the CustomDateEditor via CustomEditorConfigurer (globally), it will not works.

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
	<property name="customEditors">
	  <map>
		<entry key="java.util.Date">
			<ref local="dateEditor" />
		</entry>
	  </map>
	</property>
    </bean>

References

  1. BeanWrapperFieldSetMapper JavaDoc
  2. BeanWrapperFieldSetMapper not working for Dates
  3. java.text.SimpleDateFormat JavaDoc
  4. Spring Batch – how to convert String from file to Date
  5. Spring Inject Date Into Bean Property – CustomDateEditor
  6. How To Convert String To Date – Java
Tags :
< PreviousNext >
Spring Batch listeners exampleIndexSpring Batch metadata tables are not created automatically?
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts