Dozer Deep Property Mapping and Date Mapping Example

Dozer has Deep Mapping and String to Date Mapping available. This is a working example of using the two.

The bread and butter lives in the Dozer.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://dozer.sourceforge.net"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping>
        <class-a>com.recursivechaos.mappingtest.domain.SourceObj</class-a>
        <class-b>com.recursivechaos.mappingtest.domain.DestObj</class-b>

        <field>
            <a>nestedEnum.myEnum.value</a>
            <b>myEnum</b>
        </field>

        <field>
            <a>longDate</a>
            <b date-format="MM/dd/yyyy HH:mm:ss">longDate</b>
        </field>

        <field>
            <a>shortDate</a>
            <b date-format="MM/dd/yyyy">shortDate</b>
        </field>
    </mapping>

</mappings>

To do the deep mapping (in our case, we want to get a value from a nested enum), you just need to specify the path in the mapping like so:

<field>
    <a>nestedEnum.myEnum.value</a>
    <b>myEnum</b>
</field>

And finally, date formatting is a snap. Simply provide your date format with the field element:

<field>
    <a>longDate</a>
    <b date-format="MM/dd/yyyy HH:mm:ss">longDate</b>
</field>

This basic example can be found on GitHub

Posted in Examples Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*