This example covers 2 levels of bean copy with field level customization using Dozer mapping.If you are interestd in simple dozer example, check Simple Dozer Mapping Example

1. Create source beans

Create 2 beans AddressType.java and PersonType.java and PersonType bean is composed of AddressType

1.1 AddressType.java

package com.codesimplify.beanutils.types;

public class AddressType {

	private String addrLine1;
	private String addrLine2;
	private String city;
	private String state;
	private String zipCode;
	public String getAddrLine1() {
		return addrLine1;
	}
	public void setAddrLine1(String addrLine1) {
		this.addrLine1 = addrLine1;
	}
	public String getAddrLine2() {
		return addrLine2;
	}
	public void setAddrLine2(String addrLine2) {
		this.addrLine2 = addrLine2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	@Override
	public String toString() {
		return "AddressType [addrLine1=" + addrLine1 + ", addrLine2=" + addrLine2 + ", city=" + city + ", state="
				+ state + ", zipCode=" + zipCode + "]";
	}
	
}

1.2 PersonType.java

package com.codesimplify.dozersamples.types;

import java.util.Date;

public class PersonType {
	private String firstName;
	private String lastName;
	private String dateOfBirth;
	private String gender;
	private AddressType addrType;
	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 getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public AddressType getAddrType() {
		return addrType;
	}
	public void setAddrType(AddressType addrType) {
		this.addrType = addrType;
	}
	@Override
	public String toString() {
		return "PersonType [firstName=" + firstName + ", lastName=" + lastName + ", dateOfBirth=" + dateOfBirth
				+ ", gender=" + gender + ", addrType=" + addrType + "]";
	}

}

2. Create Target Beans

Create target beans AddressBean.java and PersonBean.java classes. PersonBean is composed of AdressBean.

2.1 AddressBean.java

package com.codesimplify.dozersamples.beans;

public class AddressBean {

	private String streetInfo1;
	private String streetInfo2;
	private String city;
	private int state;
	private String country;
	private int zip5;
	public String getStreetInfo1() {
		return streetInfo1;
	}
	public void setStreetInfo1(String streetInfo1) {
		this.streetInfo1 = streetInfo1;
	}
	public String getStreetInfo2() {
		return streetInfo2;
	}
	public void setStreetInfo2(String streetInfo2) {
		this.streetInfo2 = streetInfo2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public int getZip5() {
		return zip5;
	}
	public void setZip5(int zip5) {
		this.zip5 = zip5;
	}
	@Override
	public String toString() {
		return "AddressBean [streetInfo1=" + streetInfo1 + ", streetInfo2=" + streetInfo2 + ", city=" + city
				+ ", state=" + state + ", country=" + country + ", zip5=" + zip5 + "]";
	}
	
	
	
	
}

2.2 PersonBean.java

package com.codesimplify.dozersamples.beans;

import java.util.Date;

public class PersonBean {
	private String firstName;
	private String lastName;
	private Date dateOfBirth;
	private String gender;
	private AddressBean addrBean;
	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 Date getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public AddressBean getAddrBean() {
		return addrBean;
	}
	public void setAddrBean(AddressBean addrBean) {
		this.addrBean = addrBean;
	}
	@Override
	public String toString() {
		return "PersonBean [firstName=" + firstName + ", lastName=" + lastName + ", dateOfBirth=" + dateOfBirth
				+ ", gender=" + gender + ", addrBean=" + addrBean + "]";
	}
	

}

3. Dozer mapping xml file

Following dozer mapping xml contains the mapping information of beans and specific customization

DozerMappings.xml
<?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.codesimplify.dozersamples.types.AddressType</class-a>
	<class-b>com.codesimplify.dozersamples.beans.AddressBean</class-b>
	<field>
		<a>addrLine1</a>
		<b>streetInfo1</b>
	</field>
	<field>
		<a>addrLine2</a>
		<b>streetInfo2</b>
	</field>
	<field >
		<a>zipCode</a>
		<b>zip5</b>
	</field>	
	</mapping>
	<mapping >
	<class-a>com.codesimplify.dozersamples.types.PersonType</class-a>
	<class-b>com.codesimplify.dozersamples.beans.PersonBean</class-b>
	<field>
		<a>addrType</a>
		<b>addrBean</b>
	</field>
	<field>
		<a date-format="yyyy-MM-dd">dateOfBirth</a>
		<b>dateOfBirth</b>
	</field>
	
   </mapping>
</mappings>

In the above mapping, we can observe that date format for dateOfBirth is converted from string object in “yyyy-MM-dd” format to Date object.

4. Run Application


package com.codesimplify.dozersamples;


import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

import com.codesimplify.dozersamples.beans.PersonBean;
import com.codesimplify.dozersamples.types.AddressType;
import com.codesimplify.dozersamples.types.PersonType;

public class DozerComplexExampleApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DozerBeanMapper mapper=new DozerBeanMapper();
    	List myMappingFiles = new ArrayList();
    	myMappingFiles.add("DozerMappings.xml");
    	mapper.setMappingFiles(myMappingFiles);
    	
    	
    	PersonType persType=populatePersType();
    	PersonBean persBean=mapper.map(persType, PersonBean.class);
    	System.out.println("Source Bean:"+persType);
    	System.out.println("Destination Bean:"+persBean);
    	// Bi-Directional Conversion 
    	PersonType type2=mapper.map(persBean, PersonType.class);
    	System.out.println("Bi-Directional Conversion Test:"+type2);
	}
	private static AddressType populateAddrType(){
		AddressType addrType=new AddressType();
    	addrType.setAddrLine1("121 Howard Lane");
    	addrType.setAddrLine2("APT 222");
		addrType.setCity("Orlando");
		addrType.setState("10"); 
		addrType.setZipCode(32050);
		return addrType;
	}
	private static PersonType populatePersType(){
		PersonType persType=new PersonType();
    	persType.setFirstName("Venkat");
    	persType.setLastName("Gutta");
    	persType.setDateOfBirth("1983-01-01");
    	persType.setGender("Male");
    	persType.setAddrType(populateAddrType());
		return persType;
	}

}

Output:

Source Bean: PersonType [firstName=Venkat, lastName=Gutta, dateOfBirth=1983-01-01, gender=Male, addrType=AddressType [addrLine1=121 Howard Lane, addrLine2=APT 222, city=Orlando, state=10, zipCode=32050]]

Destination Bean: PersonBean [firstName=Venkat, lastName=Gutta, dateOfBirth=Sat Jan 01 00:00:00 CST 1983, gender=Male, addrBean=AddressBean [streetInfo1=121 Howard Lane, streetInfo2=APT 222, city=Orlando, state=10, country=null, zip5=32050]]

Bi-Directional Conversion Test:PersonType [firstName=Venkat, lastName=Gutta, dateOfBirth=1983-01-01, gender=Male, addrType=AddressType [addrLine1=121 Howard Lane, addrLine2=APT 222, city=Orlando, state=10, zipCode=32050]]

Dozer Mapping – deep java bean copy

Leave a Reply

Your email address will not be published. Required fields are marked *