[JAVA] How to use Dozer.mapper

What is Dozer

One of Java's mapping frameworks. Dozer is a mapping framework that uses recursion to copy data from one object to another. The framework can not only copy properties between beans, but also automatically convert between different types.

Benefits of Dozer

Setting method (environment)

Gradle project

Describe the following in the "build.gradle" file

Description location: dependencies Description code:

implementation 'net.sf.dozer:dozer:5.5.1'

Maven project (this time we will mainly introduce this)

Add the following to pom.xml

		<dependency>
			<groupId>net.sf.dozer</groupId>
			<artifactId>dozer</artifactId>
			<version>5.5.1</version>
		</dependency>

You can read the mapping xml to be written by adding the following to application.context.

<bean class="org.dozer.spring.DozerBeanMapperFactoryBean">
    <property name="mappingFiles"
        value="classpath*:/META-INF/dozer/**/*-mapping.xml" /><!-- (1) -->
</bean>

How to use Dozer

First, let's compare the code description with and without Dozer.mapper.

Duzer.No mapper


		input.setId(context.getId());
		input.setName(context.getName());
		input.setTitle(context.getTitle());
		input.setSubtitlle(context.getSubTitle());
		input.setBusinessDate(context.getBusinessDate());

Dozer.with mapper


		Mapper mapper = new DozerBeanMapper();
		mapper.map(context, input);

As you can see at a glance, the description has been largely omitted. This time there is no hierarchical structure of been, but even so, you do not have to write getters and setters many times to prevent omission of value copying.

I will explain in detail when I see the outline.

Been used this time Map this to the been required to call Service.

@Data
public class ControllerContext {
	private int id;
	private String name;
	private String title;
	private String subTitle;
	private Date businessDate;
}
@Data
public class ServiceContext {
	private int id;
	private String name;
	private String title;
	private String subtitlle;
	private Date businessDate;
}

Set the value to become of the controller as follows. Then map with Dozer.mapper.

		ServiceContext input = new ServiceContext();
		context.setId(2);
		context.setName("Good");
		context.setTitle("Title 2");
		context.setSubTitle("Subtitle 2");
		context.setBusinessDate(DateUtils.addDays(new Date(), 1));
		Mapper mapper = new DozerBeanMapper();
		mapper.map(context, input);
		System.out.println(input);

Output result


ServiceContext(id=2, name=Good, title=Title 2, subtitlle=subtitle, businessDate=Mon Oct 19 00:17:38 JST 2020)
Method Description
mapper.map() It can be used by describing the converted source name in the first argument and the converted been name in the second argument.

You can see that it is mapped normally. In this way, the description of been mapping can be omitted and the program can be written concisely.

Mapping when the field names are different

Dozer's default settings automatically copy properties that have the same name as the source and destination. Custom mapping is required to copy properties with different names.

Dozer settings can be defined in either XML or Java Config. It is OK if you use the one you want to use, but this time I will describe the description in the case of XML.

If you have done the setup for the first Maven project, the `-mapping.xml``` format file under `resouce / META-INF / dozer``` will be read at the time of mapping. It has become.

The mapping XML is described as follows.

<?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">
    <!-- omitted -->
    <mapping>
        <class-a>com.example.mavenDemo.controller.ControllerForm</class-a>
        <class-b>com.example.mavenDemo.service.ServiceContext</class-b>
        <field>
              <a>controllerTitle</a>
              <b>title</b><!-- (1) -->
        </field>
    </mapping>
    <!-- omitted -->
</mappings>
tag Description
class-a Specify the fully qualified class name of the copy source bean in the tag.
class-b Specify the fully qualified class name of the copy destination bean in the tag.
a <field>In the tag<a>Specify the field name for mapping of the copy source bean in the tag.
b <field>In the tag<b>Class in the tag-Specify the field name for mapping of the copy destination bean corresponding to a.

The above custom mapping settings alleviate the difference in field names.

Mapping when data types are different

Even if the data type is different, if it is an existing pattern, it will be saved automatically. See below for conversion patterns. Basic Property Mapping

Let's take a mapping example in which id of the mapping given earlier is changed to int.

@Data
public class ControllerForm {
	private String id;
	private String name;
	private String title;
	private String subTitle;
	private Date businessDate;
}

@Data
public class ServiceContext {
	private int id;
	private String name;
	private String title;
	private String subTitle;
	private Date businessDate;
}
		Mapper mapper = new DozerBeanMapper();
		mapper.map(context, input);

Execution result


ServiceContext(id=2, name=Good, title=Title 2, subTitle=サブTitle 2, businessDate=Mon Oct 19 17:12:25 JST 2020)

It can be seen that mapping is performed normally even when the data types are different.

reference

In this article, I could only introduce the touch part of Dozer mapping. See the reference article for more details.

Java Mapping Framework Performance --Codeflow Spring Boot Camp: Spring Boot + Dozer Edition How to use Dozer

Recommended Posts

How to use Dozer.mapper
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
How to use EventBus3 and ThreadMode
How to use Spring Data JDBC
How to use binding.pry [53 days left]
How to use Java HttpClient (Post)
[Java] How to use join method
How to use Ruby on Rails
How to use equality and equality (how to use equals)