This tutorial example will cover global exclude of fields in Dozer. Some fields are common across all the POJOs( For instance, audit columns like createdBy etc) and you want to skip data in those columns which are coming from Webservice consumer/UI layer. Below steps will explain how to solve such real world concern.

Steps:

  • 1. Move common fields ( like audit fields) into super class for source and target classes
  • 2. Create mapping for super and sub classes.
  • 3. Run the example

1. Move Common fields into super class for source and target POJOs

To avoid defining mapping for common fields in each of the POJO mapping, we can move such fields ( For instance, audit fields like createdUser,updatedUser,createdDate and udpatedDate etc) into super classes to avoid duplicating them in all POJO classes.

Source POJO classes


//PersonType.java
public class AddressType extends BaseType{

	private String addrLine1;
	private String city;
	private String state;
	private int zipCode;
    // Setter and Getter methods are removed
}
// BaseType.java
public class BaseType {	
        private String createdBy;
    // Setter and Getter methods are removed
}

Target POJO classes


// Address.java
public class Address extends BaseDomain{

	private String addrLine1;
	private String city;
	private String state;
	private int zip5;
// Setter and Getter methods are removed
}
//BaseDomain.java
public class BaseDomain {	
	private String createdBy;
// Setter and Getter methods are removed
}

2. Create mapping for super classes and add one-way exclude on createdBy

Create mapping for super class and sub class. In this example, create mapping between AddressType and Address domain classes and create mapping between corresponding POJO’s super classes( BaseType and BaseDomain) as well.

<?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.fieldexclude.AddressType</class-a>
			<class-b>com.codesimplify.dozersamples.fieldexclude.Address</class-b>	
	</mapping>
	
   <mapping>
	<class-a>com.codesimplify.dozersamples.fieldexclude.BaseType</class-a>
	<class-b>com.codesimplify.dozersamples.fieldexclude.BaseDomain</class-b>
	<field-exclude type="one-way">
	    <a>createdBy</a>
	    <b>createdBy</b>
	</field-exclude>	
	</mapping>
</mappings>
  • Created Mapping between sub classes AddressType and Address
  • Created Mapping between super classes i.e BaseType and BaseDomain and excluded createdBy “one-way”.This mapping results into skipping createdBy from all sub classes of BaseType to BaseDomain convertion.

3. Run the application

Below example application will skip createdBy from AddressType=>Address conversion.And createdBy will be populated during Address=>AddressType conversion.

public class App_CustomConverter 
{
    public static void main( String[] args )
    {
    	DozerBeanMapper mapper=new DozerBeanMapper();
    	List myMappingFiles = new ArrayList();
    	myMappingFiles.add("DozerMappings_global_exclude.xml");
    	mapper.setMappingFiles(myMappingFiles);
    	
    	AddressType addrType=new AddressType();
    	addrType.setAddrLine1("121 Howard Lane");
    	addrType.setCity("Orlando");
		addrType.setState("Florida");
		addrType.setZipCode(32050);
		addrType.setCreatedBy("venkat");
    	Address destAddr=mapper.map(addrType, Address.class);
    	// Convert Address Type to Address Domain
    	System.out.println("AddressType=>Address:"+destAddr.toString());
    	
    	destAddr.setCreatedBy("gutta");
    	//Convert Address domain to AddressType
    	AddressType destAddrType=mapper.map(destAddr, AddressType.class);
    	System.out.println("Address=>AddressType:"+destAddrType.toString());
    	
    }
}

Output:

AddressType=>Address:Address [addrLine1=121 Howard Lane, city=Orlando, state=Florida, zip5=0, createdBy= null]
Address=>AddressType:AddressType [addrLine1=121 Howard Lane, city=Orlando, state=Florida, zipCode=0, createdBy= gutta]

Even though there is no specific global configuration provided to skip fields, this approach allows us to achieve global exclude of fields with ease.

Dozer fields Global exclude example

Leave a Reply

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