关于Java:Spring Batch-如何将String从文件转换为Date?

Spring Batch - how to convert String from file to Date?

我正在尝试处理其中某些字段为格式为"yyyy-MM-dd"的日期的CSV文件-但是,当读者尝试将字符串从CSV文件转换为模型类中的Date时,阅读器失败。铅>

错误是:

org.springframework.validation.BindException:
org.springframework.validation.BeanPropertyBindingResult: 1 error
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]

阅读器的XML:

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

在我的XML配置文件中,我具有以下bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <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文档中找不到与此相关的任何示例代码。你能在这里展示如何做/向我指出一些链接吗?

  • 更新:

    我对问题2有一个答案:

    XML:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
      <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>

    自定义转换器:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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。从这个论坛线程看来,我的代码应该已经执行了转换。

    堆栈跟踪为:

    1
    2
    3
    4
    5
    6
    7
    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

    您的论坛参考用于在构建应用程序上下文和配置Bean时进行类型转换。

    看看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*, or you can override the createBinder(Object) and
    initBinder(DataBinder) methods, or you can provide a custom FieldSet
    implementation.

    意味着您应该将CustomDateEditor直接注入Mapper