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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class SourceBean { private int id; private String fullName; private String city; private String zipCode; ...... } |
We will create the Destination bean with some changes to the variable names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class DestinationBean { private String primaryKey; private String name; private String city; private String postalCode; .......... // Setters and Getters } |
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.
1 2 3 4 |
<field> <a>id</a> <b>primaryKey</b> </field> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>com.kscodes.dozer.SourceBean</class-a> <class-b>com.kscodes.dozer.DestinationBean</class-b> <field> <a>id</a> <b>primaryKey</b> </field> <field> <a>fullName</a> <b>name</b> </field> </mapping> </mappings> |
Now lets call this xml file. Dozer will check for classpath for this xml. We can add it directly from the drive using file:\\.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kscodes.dozer; import java.util.ArrayList; import java.util.List; import org.dozer.DozerBeanMapper; public class SimpleDozerTest { public static void main(String args[]) { List<String> mappingFileUrls = new ArrayList<>(); mappingFileUrls.add("file:\\K:\\Kscodes\\workspace\\DozerSampleProject\\resources\\employee-mapper.xml"); // Create a Mapper Object DozerBeanMapper mapper = new DozerBeanMapper(); SourceBean sourceBean = new SourceBean(); sourceBean.setId(1); sourceBean.setFullName("John Doe"); sourceBean.setCity("San Francisco"); sourceBean.setZipCode("90912"); mapper.setMappingFiles(mappingFileUrls); // Use the mapper Object and then map the required object DestinationBean destBean = mapper.map(sourceBean, DestinationBean.class); System.out.println(destBean); } } |
Output
You will see that everything except postalCode was not mapped, which was expected.
References
1. Dozer XML mappings