Set Up Mapstruct

In this lesson, we'll integrate Mapstruct with our Restaurant Review Platform to efficiently handle object mapping between different layers of our application, while ensuring it works smoothly with Project Lombok.

Understanding Mapstruct and Lombok Integration

Mapstruct is a code generator that helps us create type-safe bean mappings between Java bean classes.

When using Mapstruct alongside Lombok, we need special configuration to ensure both tools work together properly during the compilation process.

The order of annotation processing is important - Lombok must process its annotations before Mapstruct can generate its mapper implementations.

Configuring Dependencies

Let's start by adding the required version properties and dependencies to our pom.xml file.

First, we'll add the version properties:

<properties> <org.mapstruct.version>1.6.3</org.mapstruct.version> <lombok.version>1.18.36</lombok.version> </properties>

We'll need to pin the Lombok version to ensure it works with Mapstruct:

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency>

Next, we'll add the Mapstruct dependency:

<dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency>

Setting up the Maven Compiler Plugin

The Maven Compiler Plugin needs specific configuration to handle both Lombok and Mapstruct annotation processing:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <!-- Lombok must come first --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <!-- Mapstruct processor --> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> <!-- Lombok-Mapstruct binding --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> </path> </annotationProcessorPaths> </configuration> </plugin>

Understanding the Configuration

The annotationProcessorPaths section defines the order of annotation processing:

  1. Lombok processes its annotations first
  2. Mapstruct processes its annotations second
  3. The Lombok-Mapstruct binding ensures compatibility between both tools

There's some conflicting information about if the order is required or not, but I found that it is needed.

Summary

  • Added Mapstruct dependency and version property to the project
  • Configured Maven Compiler Plugin for annotation processing
  • Set up Lombok-Mapstruct binding for compatibility
© 2026 Devtiro Ltd. All rights reserved