Saturday, 5 March 2022

Dozer: Map only specific fields

By setting ‘wildcard’ attribute to false, we can restrict the dozer to map explicitly defined fields.

 

Example

<mapping wildcard="false">
	<class-a>com.sample.app.model.Employee</class-a>
	<class-b>com.sample.app.model.Person</class-b>

	<field>
		<a>fName</a>
		<b>firstName</b>
	</field>

</mapping>

In the above snippet, I set the attribute ‘wildcard’ to false (wildcard is set to true by default, This means that it will automatically try to map every property in the two objects.), when dozer reads this property, it can understand that user want to map only defined fields.


 

Find the below working application.

 

Employee.java

package com.sample.app.model;

public class Employee {
	private Integer id;
	private String fName;
	private String lName;
	
	public Employee() {}

	public Employee(Integer id, String fName, String lName) {
		this.id = id;
		this.fName = fName;
		this.lName = lName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", fName=" + fName + ", lName=" + lName + "]";
	}
	
	

}

Person.java

package com.sample.app.model;

public class Person {
	private Integer id;
	private String firstName;
	private String lastName;

	public Person() {
	}

	public Person(Integer id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

map-defined-fields-only.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 wildcard="false">
		<class-a>com.sample.app.model.Employee</class-a>
		<class-b>com.sample.app.model.Person</class-b>

		<field>
			<a>fName</a>
			<b>firstName</b>
		</field>

	</mapping>

</mappings>

MapSpecificFieldDemo.java

package com.sample.app;

import java.io.IOException;
import java.util.Arrays;

import org.dozer.DozerBeanMapper;

import com.sample.app.model.Employee;
import com.sample.app.model.Person;

public class MapSpecificFieldDemo {

	public static void main(String[] args) throws IOException {

		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.setMappingFiles(Arrays.asList("map-defined-fields-only.xml"));

		Employee employee = new Employee(1, "Arjun", "Gurram");
		Person person = mapper.map(employee, Person.class);

		System.out.println(employee);
		System.out.println(person);

	}

}

Output

Employee [id=1, fName=Arjun, lName=Gurram]
Person [id=null, firstName=Arjun, lastName=null]

As you see above snippet, only fName is mapped by Dozer.





  

Previous                                                 Next                                                 Home

No comments:

Post a Comment