This article covers simple dozer mapping example.

Advantages of dozer:

  • Allows Java Bean copy even though field names are different. BeanUtils only support if the names are matching.
  • Allows configurablity
  • Allows custom convertion
  • Supports child object copy.
  • Supports Deep copy

Dozer setup:

If you are using maven, following dependency to be added to pom.xml

	<dependency>
	  <groupId>net.sf.dozer</groupId>
	  <artifactId>dozer-parent</artifactId>
	  <version>5.4.0</version>
	  <type>pom</type>
	</dependency>

1. Create Source Bean

package com.codesimplify.dozersamples.types;

public class AddressType {

	private String addrLine1;
	private String addrLine2;
	private String city;
	private String state;
	private int 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 int getZipCode() {
		return zipCode;
	}
	public void setZipCode(int zipCode) {
		this.zipCode = zipCode;
	}
	@Override
	public String toString() {
		return "AddressType [addrLine1=" + addrLine1 + ", addrLine2=" + addrLine2 + ", city=" + city + ", state="
				+ state + ", zipCode=" + zipCode + "]";
	}
}

2. Create Target Bean

package com.codesimplify.dozersamples.beans;

public class AddressBean {

	private String streetInfo1;
	private String streetInfo2;
	private String city;
	private String 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 String getState() {
		return state;
	}
	public void setState(String 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 + "]";
	}
}

3. Create Dozer Mapping xml

If you observe below dozer mapping file, we can easily understand how dozer allows to map fields with different names.We can also observe following points.

    Only Mapped fields with different names in source and target beans
    Type conversions are handled by dozer. For instance, zipCode( int) to zip5( String).
AddressMapping.xml
	<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>
	</mappings>

4. Copy beans using Dozer

package com.codesimplify.dozersamples;

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

import org.dozer.DozerBeanMapper;

import com.codesimplify.dozersamples.beans.AddressBean;
import com.codesimplify.dozersamples.types.AddressType;

public class App 
{
    public static void main( String[] args )
    {
    	DozerBeanMapper mapper=new DozerBeanMapper();
    	List myMappingFiles = new ArrayList();
    	myMappingFiles.add("AddressMapping.xml");
    	mapper.setMappingFiles(myMappingFiles);
    	
    	AddressType addrType=new AddressType();
    	addrType.setAddrLine1("121 Howard Lane");
    	addrType.setAddrLine2("APT 222");
		addrType.setCity("Austin");
		addrType.setState("Florida");
		addrType.setZipCode(32050);
    	AddressBean destAddrBean=mapper.map(addrType, AddressBean.class);
        System.out.println("Source Bean:"+addrType);
    	System.out.println("Destination Bean:"+destAddrBean);	       
    }
}

Output:

Source Bean:AddressType [addrLine1=121 Howard Lane, addrLine2=APT 222, city=Austin, state=Florida, zipCode=32050]
Destination Bean:AddressBean [streetInfo1=121 Howard Lane, streetInfo2=APT 222, city=Austin, state=Florida, country=null, zip5=32050]

NOTE: In real world applications, we should create DozerBeanMapper once ( Singleton pattern).
Simple Dozer mapping example

One thought on “Simple Dozer mapping example

Leave a Reply

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