Lets see Dozer mapper simple example, where we will map a java object into another using the DozerBeanMapper class.
Dozer mapper simple example
We will have SourceBean and a DestinationBean. Both of them will have similar variables. We will use the Dozer mapper to map data from SourceBean to DestinationBean
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.kscodes.dozer; public class SourceBean { private int id; private String firstName; private String lastName; private String destination; private String address1; private String city; private float salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getAddress1() { return address1; } public void setAddress1(String address1) { this.address1 = address1; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } } |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.kscodes.dozer; public class DestinationBean { private int id; private String firstName; private String lastName; private String destination; private String address1; private String city; private float salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getAddress1() { return address1; } public void setAddress1(String address1) { this.address1 = address1; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } @Override public String toString() { return "DestinationBean [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", destination=" + destination + ", address1=" + address1 + ", city=" + city + ", salary=" + salary + "]"; } } |
Mappper Test Class
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 |
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.setFirstName("John"); sourceBean.setLastName("Doe"); sourceBean.setAddress1("1 Main St"); sourceBean.setCity("San Francisco"); sourceBean.setSalary(4500.00f); // Use the mapper Object and then map the required object DestinationBean destBean = mapper.map(sourceBean, DestinationBean.class); System.out.println(destBean); } } |
So as you can see just couple of lines code ( highlighted in the code snippet above) can map one object to another.
Output
Please Note:
We do not need to create the Mapper object everytime we need to map something. Instead you can create a Singleton object and use it across the application.