How to Use MapStruct with Lombok’s Builder

AJIL ANTO | November 02, 2022

How to Use MapStruct with Lombok’s Builder
How to Use MapStruct with Lombok’s Builder | Sanesqure Technologies

Are you still leaving your office late at night or stuck on an existing project by spending valuable time writing boilerplate codes for bean mapping in your powerful multi-layered architecture application? Now, it is time for you to master a new magic spell, which can help you reduce the time that is being wasted by writing many boilerplate codes for bean mapping.

Let me introduce MapStruct, A powerful annotation processor provided by Java. Which helps you to reduce the amount of extra boilerplate code for bean mapping. As of now, MapStruct version 1.5.2 final is the very latest version, which was released on June 18, 2022. It is possible to integrate it with the Lombok Builder pattern for better performance.

Generator Libraries Required

  • Lombok is Automatic Resource Management, automatic generation of getters, setters, equals, hash Code and to String, and more.
  • MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach.

These simple snippets of code will make you aware of how these two libraries work hand in hand in the way MapStruct implements Lombok’s builder methods. Implementing the Lombok builder which will help us to make our code bug-free and maintenance-free. And which will increase the overall quality of the code by reducing unwanted lines of code.

Step 1: If you are using the maven project, make these necessary changes in the pom.xml.

blog mapstruct image 01

If you are using the Gradle project make changes in the build. Gradle.

blog mapstruct image 02

Step 2: After making changes in properties, we need to add a couple of dependencies.

blog mapstruct image 03

Step 3: Configure the maven-compile plugin to auto-generate implementations with the maven command.

blog mapstruct image 04


To Achieve MapStruct functionality, we need to create an Interface, which will act as a bridge between the POJOs consisting of @mapper annotation. (@mapper: this annotation tells the MapStruct to generate an implementation class for the given interface).

Consider an example, where we create an entity named as ProductDetails and a ProductResponseDTO and we want to map the fields between them. Compared to other mapping frameworks, MapStruct provides us with the following benefits.

  1. Instead of reflection MapStruct provides fast execution by using plain method invocations.
  2. This provides compile-time type safety. The objects and attributes which share mapping can only be mapped using a MapStruct.
  3. MapStruct provides clear build-time reports if any occurred while the time of compiling.

blog mapstruct image 05

blog mapstruct image 06

To achieve mapping between the fields in both java classes we need to create an interface that has been mentioned above that consists of @mapper annotation. And we specify the input and the output.


blog mapstruct image 07

After doing these many steps we need to compile the maven.

When these steps are completed, a new class is generated ProductMapperImpl. Which can be found under the target folder.

blog mapstruct image 08


blog mapstruct image 09

For Mapping, the list of entities

Considering that list of items was there inside the ProductDetails and we want to convert it into ProductResponseDTO we need to configure our mapper interface with new methods as shown in the figure.

blog mapstruct image 10

After configuring the mapper interface compile the maven as we did before. And follow the previous steps.

For mapping entities of different fields, names

Consider two new entities ProductDetails and ProductResponseDTO


blog mapstruct image 11


blog mapstruct image 12

After successfully creating these two entities as we did in the previous case, configure the mapper interface with an extra annotation called @Mapping.

If we need to define the field which differs in both entities.

  • Source: source contains the name we have given inside the input entity.
  • Target: target contains the name we have given inside the output entity.


blog mapstruct image 13

In this world, everyone is in hurry and we need some new measures to complete our tasks in the prescribed time without making any further lag in the projects which we are handling. If you are an IT professional or student who cares more about an IT career, I would surely recommend mastering the MapStruct which will make your way faster and better.

Thanks for reading! In this blog, we tried to convey a basic introduction to MapStruct and we expect all of you to understand what we tried to convey. If you run into any trouble or need assistance with MapStruct, get in touch with us.

Does your Project Demand Expert Assistance?

Contact us and let our experts guide you and fulfil your aspirations for making the project successful