Effectively using MapStruct and Lombok

Erol Yapıcı
2 min readAug 20, 2022

MapStruct and Lombok are libraries, which can make developer’s life as much easier. Because, MapStruct and Lombok save time to developers.

What is MapStruct?

MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.

What is Lombok?

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, automate your logging variables, and much more.

Using MapStruct with Lombok

  1. Create lombok.config in the root of our project. It will be automatically be picked up.
lombok.addLombokGeneratedAnnotation=true
lombok.anyConstructor.addConstructorProperties=true

Initial Maven Dependencies

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

Maven Annotation Processing Configuration

We need to configure the Maven Compiler plugin to support the annotation processors of MapStruct and Project Lombok. Make sure versions are match each other.

If you are using Lombok 1.18.16 or newer you also need to add lombok-mapstruct-binding in order to make Lombok and MapStruct work together.

Maven-compiler-plugin’s version has to be greater than 3.8.0

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

If you want to create bean all mapper, you can add -Amapstruct.defaultComponentModel=spring on compilerArg
or @Mapper(componentModel = “spring”)

Lets create Dtos

@Getter
@AllArgsConstructor
@Builder
class UserRequest extends BaseRequest{
private String id;
private String name;
}
@Getter
@AllArgsConstructor
@SuperBuilder
class UserEntity extends BaseEntity{
private String id;
private String name;
}

If you want to use builder pattern with MapStruct, you have to use @SuperBuilder for complies the extended properties. Otherwise MapStruct couldn’t compile extented properties.

Let’s create Interface

@Mapper(componentModel = "spring")//Creates a Spring Bean automatically
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

UserEntity requestToEntity(UserRequest request);
}

Now run the maven build and have a look at the implementation UserMapperImpl

--

--

Erol Yapıcı

Love Java, PHP, JavaScript, Python. Scrum Master, Team Lead