This tutorial covers Dozer’s Custom Converter usage example.

1. Create Source and Target POJOs

Create simple Address source and target beans

Source POJO: AddressType.java

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 + "]";
	}
	
}

Target POJO: Address.java

package com.codesimplify.dozersamples.domain;

public class Address {

	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 + "]";
	}
	}

If you notice, we defined state code in source bean as String type and target bean as int. We need to convert text format of state field to int to store into database.

2. Create Dozer Custom Converter

To convert specific state name to corresponding int value, define following Custom converter.

StateCodeConverter.java
package com.codesimplify.dozersamples.converters;

import org.dozer.DozerConverter;

public class StateCodeConverter extends DozerConverter{

	public StateCodeConverter() {
		super(String.class, Integer.class);
	}

	@Override
	public Integer convertTo(String source, Integer destination) {
		if(source!=null && "Florida".equalsIgnoreCase(source)){
			return 10;
		}
		return null;
	}

	@Override
	public String convertFrom(Integer source, String destination) {
		if(source!=null && source.intValue()==10){
			return "Florida";
		}
		return null;
	}
}

3. Dozer Mapping xml with custom converter at field level

Defined StateCodeConverter is configured for state field

DozerMappings.xml
package com.codesimplify.dozersamples;

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

import org.dozer.DozerBeanMapper;


import com.codesimplify.dozersamples.domain.Address;
import com.codesimplify.dozersamples.types.AddressType;

public class CustomConverterApp 
{
    public static void main( String[] args )
    {
    	DozerBeanMapper mapper=new DozerBeanMapper();
    	List myMappingFiles = new ArrayList();
    	myMappingFiles.add("DozerMappings.xml");
    	mapper.setMappingFiles(myMappingFiles);
    	
    	AddressType addrType=new AddressType();
    	addrType.setAddrLine1("121 Howard Lane");
    	addrType.setAddrLine2("APT 222");
		addrType.setCity("Orlando");
		addrType.setState("Florida");
		addrType.setZipCode(32050);
    	Address destAddr=mapper.map(addrType, Address.class);
    	System.out.println("Source POJO:"+addrType);
    	System.out.println("Destination Domain:"+destAddr);  
    	//Bi-Directional Conversion
    	AddressType reverseConvType=mapper.map(destAddr, AddressType.class);
    	System.out.println("Reverse Conversion :"+reverseConvType); 
    }
}

Output:

Source POJO: AddressType [addrLine1=121 Howard Lane, addrLine2=APT 222, city=Orlando, state=Florida, zipCode=32050]
Destination Domain:AddressBean [streetInfo1=121 Howard Lane, streetInfo2=APT 222, city=Orlando, state=10, country=null, zip5=32050]
Reverse Conversion: AddressType [addrLine1=121 Howard Lane, addrLine2=APT 222, city=Orlando, state=Florida, zipCode=32050]

Dozer Mapping – Simple Custom Converter

Leave a Reply

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