Sunday 6 March 2022

Dozer: Specify xml mappings from input stream

Using ‘addMapping’ method, we can specify xml mappings from an input stream.

 

Example

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

 

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

}

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>

BeanMappingWithInputStream.java

package com.sample.app;

import java.io.IOException;
import java.io.InputStream;

import org.dozer.DozerBeanMapper;

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

public class BeanMappingWithInputStream {

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

		DozerBeanMapper mapper = new DozerBeanMapper();

		ClassLoader classloader = Thread.currentThread().getContextClassLoader();
		InputStream inputStream = classloader.getResourceAsStream("employee-person-mapping.xml");

		mapper.addMapping(inputStream);
		
		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