Search code examples
javadozer

I am using dozer mapper. How i can set my default value to field if it is empty?


i need co convert a lot of string fields, and if field is empty, i want to set my own value. How can i do it? Write setters to all fields, i think it's not the best solution. Are there options in dozer mapper, for this operation?
Also i can write my own converter, but i want to know, is there any universal solutions?


Solution

  • You can set map-null and map-empty-string to false to bypass the mapping of null or empty String values. For example:

    <mapping map-null="false" map-empty-string="false">
      <class-a>org.dozer.vo.AnotherTestObject</class-a>
      <class-b>org.dozer.vo.AnotherTestObjectPrime</class-b>    
      <field>
        <a>field4</a>
        <b>to.one</b>
      </field>
    </mapping>  
    

    However, if you want to set it to your own default value, then I think you will have to write a Custom Converter.