Traditional use of Dozer Mapping was done using the XML configurations. But using XML and configuring fields can be a painful job for developers. This also creates code duplication and we need to map the same fields in both to and from classes for the simplest of mappings as well. To avoid this we can use the Annotations feature provide by Dozer.
How to use Annotation in Dozer Mapping
You can use the @Mapping annotation on
a. Getter of the field OR
b. Field directly
Dozer Mappers adds a bi-directional mapping when it finds the field. Annotations also handle the conversions automatically.
Lets see an example where we will see how the fields can be converted using Annotation
Annotation Example
In this example we will see different ways in which mapping can be done using annotation in Dozer mapping.
1. Automatic mapping of fields with same names.
2. Field level use of Annotations
3. Getter method level use of Annotations
4. Automatic type conversion.
Please see the highlighted comments in the below example to show you the various use of Annotations
1. SourceBean.java
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.kscodes.dozer; import org.dozer.Mapping; public class SourceBean { private int id; // Direct Field level mapping @Mapping("name") private String fullName; // Maping happens directly as the destination bean has same field name. private String city; @Mapping("postalCode") private String zipCode; // Getter level mapping; // Automatic Type conversion happens from int to String @Mapping("primaryKey") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } } |
2. DestinationBean.java
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.kscodes.dozer; public class DestinationBean { private String primaryKey; private String name; private String city; private String postalCode; public String getPrimaryKey() { return primaryKey; } public void setPrimaryKey(String primaryKey) { this.primaryKey = primaryKey; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPostalCode() { return postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } @Override public String toString() { return "DestinationBean [primaryKey=" + primaryKey + ", name=" + name + ", city=" + city + ", postalCode=" + postalCode + "]"; } } |
3. SimpleDozerTest.java
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 |
package com.kscodes.dozer; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; public class SimpleDozerTest { public static void main(String args[]) { // Create a Mapper Object Mapper mapper = new DozerBeanMapper(); SourceBean sourceBean = new SourceBean(); sourceBean.setId(1); sourceBean.setFullName("John Doe"); sourceBean.setCity("San Francisco"); sourceBean.setZipCode("90912"); // Use the mapper Object and then map the required object DestinationBean destBean = mapper.map(sourceBean, DestinationBean.class); System.out.println(destBean); } } |
Output
When to Avoid Annotation in Dozer Mapping
1. You are mapping classes, which are not under your control, but provided in libraries.
2. The mappings are quite complex and require many configurations.