Member-only story

Automating Code with MapStruct

Swathi Prasad
5 min readApr 28, 2019

Often, we need to convert domain model to DTO (Data transfer object) and vice versa. This is necessary when transferring data to frontend or remote interfaces. Dealing with complex mappings manually becomes cumbersome and may result in errors.

In this article, I would like to introduce MapStruct, a Java annotation processor that generates mapper implementations for Java beans at compile time. It uses plain Java method invocations for mapping objects and no reflection or runtime processing is involved.

I will walk you through the steps to integrate MapStruct in a Spring Boot application.

Maven Dependency

Let’s create a sample Spring Boot application and add MapStruct and its processor dependencies in pom.xml.

The mapstruct-processor is used for generating mapper implementation during compilation.

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
<scope>provided</scope>
</dependency>

Creating JPA Entities and DTOs

--

--

Swathi Prasad
Swathi Prasad

Written by Swathi Prasad

Software architect and developer living in Germany. Sharing my opinion and what I learn.

Responses (5)