Announcement Announcement Module
No announcement yet.
Data Type converters with BeanWrapperFieldSetMapper Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data Type converters with BeanWrapperFieldSetMapper

    Spring Batch experts,

    I am using BeanWrapperFieldSetmapper but it throws exception at converting empty strings into double variable. I looked at the customEditors but I don't quite get how I can put type converters like string to double.Please point to some example where data conerters or DataBinders are used with BeanWrapperFieldSetMapper.


  • #2
    BeanWrapperFieldSetMapper has a protected method initBinder(DataBinder binder). Do your custom type conversion there. The PropertyEditors from the application context are also used by default, so if it is a global setting you could register there as well.


    • #3

      I need similar behavior because my feed file has certain double value columns that can be empty. And the double value columns map to my prototype bean's attributes that are declared as double (primitive type).

      When a double value column on the feed file is empty, I need to simply skip the setter method call on the prototype bean. Meaning, there is nothing to set on the prototype bean and since the attribute's type is primitive, I can't set it to null.

      I am having a couple of issues following your suggestion.

      1. Spring Batch 2.0.1 source code for BeanWrapperFieldSetMapper.initBinder() method begins with a comment that specifically instructs against registration of custom editors in this method. Instead, it recommends doing this registration in registerCustomEditors() method. Is that what you had meant to say?

      2. Whether it is initBinder() or registerCustomEditors(), I will need a property editor that can do String --> double (primitive type) conversion. So I have got a custom editor where I have implemented setAsText() such that it doesn't call setValue() when the field value read from the feed file is empty. This causes the "value" in editor to remain null. But null is not assignable to a primitive class ( line 217) and I get the following exception:

      Caused by: org.springframework.batch.item.file.mapping.BindingException: [Failed to convert property value of type [java.lang.String] to required type [double] for property 'quantity'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [double] for property 'quantity': PropertyEditor [com.mycompany.batch.propertyeditors.CustomPrimitiveDoubleEditor] returned inappropriate value]
              at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(
              at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(
              ... 31 more
      Is there anything that I can do to get the required behavior? My constraint is that I don't want to change the prototype bean to use Double instead of double.