Wednesday, 2 March 2022

Dozer: Specify bean mappings via xml file

In this post, I am going to explain how to specify bean mappings via xml file.

 

Define Employee class.

public class Employee {
	private Integer id;
	private String fName;
	private String lName;

	.....
	.....
}

 

Define Person class.

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

	.....
	.....
}

 

As you see Employee and Person class, both classes have a common property id, which can be auto mapped by dozer. But we need to explicitly specify the mapping logic to the properties fName, lName, firstName and lastName.

 

One way to specify custom mapping is via xml file.

 

employee-person-mapping.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.sample.app.model.Employee</class-a>
		<class-b>com.sample.app.model.Person</class-b>

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

		<field>
			<a>lName</a>
			<b>lastName</b>
		</field>

	</mapping>

</mappings>

 

As you see above definition, I want fName to be mapped to firstName and lName to be mapped to lastName.

 

A mappings element has multiple mapping elements, each with class mapping declarations and field level mappings.

 

Set the mapping file to DozerBeanMapper instance before triggering map method.

 

DozerBeanMapper mapper = new DozerBeanMapper();

mapper.setMappingFiles(Arrays.asList("employee-person-mapping.xml"));

 

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

}

 

BeanMappingWithXMLConfig.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 BeanMappingWithXMLConfig {

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

		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.setMappingFiles(Arrays.asList("employee-person-mapping.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=1, firstName=Arjun, lastName=Gurram]

 


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment