Use fast Mapping library MapStruct with Lombok and Java 11

What is MapStruct?

A library that helps convert Entity (or Domain) to DTO. Famous Mapping libraries include BeanUtils from Structs1.x and ModelMapper / Dozer.

The feature of MapStruct is that the code is automatically generated using the annotation processor like Lombok. The feature is that it is as fast as mapping using getters / setters on its own.

The performance is introduced in the following article. Performance of Java Mapping Frameworks

It's subjective, but I find the code intuitive and easy to understand, not only in terms of performance, but also when compared to other mapping tools.

It's such a nice mapping tool, but I've been stuck with it for a while & I couldn't find a similar article, so I'm writing this article.

environment

According to the Lombok Changelog, 1.8.2 seems to fix a bug in Integration with MapStruct.

・ Pleiades Eclipse 2018-09 (* Lombok 1.8.2) ・ AdoptOpenJDK 11 ・ Lombok 1.8.2 -MapStruct 1.2.0.Final and 1.3.0.Beta1

OK pattern settings

In conclusion, it works with the sample code in mapstruct-lombok and It didn't work in the MapStruct Readme (I was addicted to it).

Describe the pom as follows, ** Select a project in eclipse and run ⇒ maven install or maven test ** will generate a coat The test code provided in mapstruct-lombok succeeds.

Unfortunately, eclipse's automatic builds and clean builds don't generate code: sob: IDE Support is available and is designed to run "annotation processors" automatically. ** This is because if you set the essential "annotation processors" to Maven, it will not work: innocent: **

pom.xml



    <properties>
        <org.mapstruct.version>1.3.0.Beta1</org.mapstruct.version> ※ 1.2.0.Confirmed that Final also works
        <org.projectlombok.version>1.18.2</org.projectlombok.version>
    </properties>
<!--abridgement-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>* It will be different from the Readme
            <version>${org.mapstruct.version}</version>
            <scope>provided</scope>* It is described to be executed at compile time.
        </dependency>
<!--abridgement-->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>* Annotation described in the point Readme-processor not listed
                    <version>3.6.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

Typical example of NG pattern

pom.xml


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.6</source> <!-- or higher, depending on your project -->
                <target>1.6</target> <!-- or higher, depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>* ← At least, it doesn't seem to work on Java 11.
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

About the influence of Java 11

In the official documentation, the using_mapstruct_on_java_9 section states that "java.annotations.common" should be enabled. The java.annotations.common was Java 9 and was renamed to java.xml.ws .annotation, ** Wow, in Java 11, java.xml.ws.annotation has been removed: innocent:: innocent:: innocent: **

For now, if you write the OK pattern pom.xml and run the maven build, the code will be generated We have confirmed that it works as expected, but please understand that there may be risks.

Summary

This article has described how to use MapStruct with lombok and Java 11. For the actual usage of MapStruct, please refer to other Qiita articles and the sample code of the head family.

There are some points that fit, but It's hundreds of times more fun to write code than writing getters / setters on your own to reduce the chance of bugs.

I hope you can contribute a little to your Java life.

Recommended Posts

Use fast Mapping library MapStruct with Lombok and Java 11
Use java with MSYS and Cygwin
Use JDBC with Java and Scala.
JAVA object mapping library
Use Lambda Layers with Java
Bean mapping with MapStruct Part 1
Bean mapping with MapStruct Part 3
Bean mapping with MapStruct Part 2
Use SpatiaLite with Java / JDBC
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Use Microsoft Graph with standard Java
Use Git with SourceTree and Eclipse
Use Azure Bing SpellCheck with Java
Use Java 11 with Google Cloud Functions
Java cache library and Eviction Policy
Output PDF and TIFF with Java 8
Introducing Lightsleep, an O / R mapping library that works only with Java Runtime and JDBC drivers
Encrypt with Java and decrypt with C #
Monitor Java applications with jolokia and hawtio
Call Java library from C with JNI
Link Java and C ++ code with SWIG
Java Artery-Easy to use unit test library
Let's try WebSocket with Java and javascript!
[Java] Use cryptography in the standard library
[Java] Reading and writing files with OpenCSV
[JaCoCo (Java Code Coverage)] Use with NetBeans
[Java] How to use FileReader class and BufferedReader class
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
How to use Java framework with AWS Lambda! ??
JSON with Java and Jackson Part 2 XSS measures
I want to use java8 forEach with index
How to use Java API with lambda expression
Use Matplotlib from Java or Scala with Matplotlib4j
Prepare a scraping environment with Docker and Java
Mock and stub with minitest (use RR, WebMock, MiniTest :: Mock)
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Use Java external library for the time being
KMS) Envelope encryption with openssl and java decryption
Encrypt / decrypt with AES256 in PHP and Java
[Java] Convert and import file values with OpenCSV
[Review] Reading and writing files with java (JDK6)
[Java] How to use Calendar class and Date class
[Kotlin] The mapping library by reflection based on Java basically does not work with Kotlin.
Code to use when you want to process Json with only standard library in Java