Dozer bean mapping tutorial

Dozer is a framework that maps fields from source bean to target bean.

Practical scenarios to map source bean to target bean?

1) Applications A and B might be having different contracts and objects need to be converted from A to B. For example, Mapping JAXB Objects to Business/Domain Objects.

2) The source bean might have a lots of fields and target bean might require only a few fields.

3) The source bean might be heavily nested and needs to be flattened out as a target bean.

Where does mapping frameworks like Dozer frameworks shine?

Say, you have a deeply nested object graph like a.getB().getC().getD(), doing it via your code can become unsightly with all the null checks. With Dozer, you can elegantly map it with an XML file

Step 1: Create a simple Maven project from a command-line. Just press enter for all the prompts.

Step 2: Import the above “simple-dozer” folder in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-dozer” folder that was created by the “Step 1” from the file system.

Step 3: Open the pom.xml file and add the dependency for Dozer

Step 4: Define the source and target beans.

Source Bean

Target Bean

Step 5: The mapping definition file “dozer-bean-mappings.xml” in the src/main/resources folder.

Step 6: Finally, the class with the main method to create a source bean and map it to the target bean.

Output

As you can see the target properties were null before mapping.

Project structure

Dozer tutorial

Dozer tutorial

Alternative frameworks for bean mapping?

Orika Bean Mapping


Java Interview FAQs

800+ Java Interview Q&As

Top