Using XML configurations in Dozer Mapping

Dozer mapper supports both mapping via Annotations and XML configurations.In our previous posts we have seen how Annotations are used in Dozer. Lets see an example on simple mapping using XML configurations in Dozer Mapping.

If the two different types of data objects that you are mapping contain any fields that don’t share a common property name, you will need to add a class mapping entry to your custom mapping xml file. These mappings xml files are used at runtime by the Dozer mapping engine.
Dozer automatically performs any type conversion when copying the source field data to the destination field. The Dozer mapping engine is bi-directional, so if you wanted to map the destination object to the source object, you do not need to add another class mapping to the xml file.

Example

Our Source bean will look like this.

We will create the Destination bean with some changes to the variable names.

Lets have our XML configuration. Here we will specify that we need to map id – primaryKey and fullName – name
Since city has same naming in both beans, it will get mapped automatically.
We will not map zipCode/postalCode to check if that gets mapped automatically (Shouldn’t happen though!)

The source class here is named as a – Anything named after class-
if you see the tag <class-a>com.kscodes.dozer.SourceBean</class-a>

Fields are mapped using below XML configuration.

Now lets call this xml file. Dozer will check for classpath for this xml. We can add it directly from the drive using file:\\.

Output
Using XML configurations in Dozer Mapping

You will see that everything except postalCode was not mapped, which was expected.

References
1. Dozer XML mappings