Wednesday, 2 March 2022

Dozer: Specify bean mapping using BeanMappingBuilder

We can configure the bean mappings using ‘BeanMappingBuilder’ class.

 

Step 1: Define an instance of BeanMappingBuilder.

BeanMappingBuilder beanMappingBuilder = new BeanMappingBuilder() {

	@Override
	protected void configure() {
		mapping(Employee.class, Person.class)
		.fields("fName", "firstName")
		.fields("lName", "lastName");

	}
};

 

Step 2: Define DozerBeanMapper instance and add mapping to BeanMappingBuilder instance.

DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(beanMappingBuilder);

 

 


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

}

BeanMappingBuilderDemo.java

package com.sample.app;

import java.io.IOException;

import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;

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

public class BeanMappingBuilderDemo {

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

		BeanMappingBuilder beanMappingBuilder = new BeanMappingBuilder() {

			@Override
			protected void configure() {
				mapping(Employee.class, Person.class)
				.fields("fName", "firstName")
				.fields("lName", "lastName");

			}
		};

		DozerBeanMapper mapper = new DozerBeanMapper();
		
		mapper.addMapping(beanMappingBuilder);

		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