Set up MapStruct

In our previous lessons, we set up the database infrastructure for our blog platform.

We'll now integrate MapStruct, a code generator that simplifies the conversion between our domain entities and DTOs (Data Transfer Objects).

We'll only be setting up MapStruct in this lesson, we'll cover using MapStruct later on in the course.

Understanding MapStruct

MapStruct is a compile-time annotation processor that generates type-safe mapping code.

The generated code performs direct property mapping, avoiding the performance overhead of reflection-based mapping frameworks.

MapStruct integrates with Lombok and Spring, making it an ideal choice for our blog application, but it will need a little configuring.

Adding Dependencies

First, we need to add MapStruct dependencies to our pom.xml file. Note we're specifying the Lombok version here -- we need to do this to make sure the versions of MapStruct and Lombok we have are compatible.

<properties> <org.mapstruct.version>1.6.3</org.mapstruct.version> <lombok.version>1.18.36</lombok.version> </properties> <dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency> </dependencies>

Configuring the Compiler Plugin

The Maven compiler plugin needs special configuration to work with both MapStruct and Lombok:

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>

That's all we'll need to do for the moment. We can now use MapStruct to generate DTOs when we need them later on in the build!

Summary

  • MapStruct generates efficient bean mapping code at compile-time, eliminating runtime reflection overhead
  • Proper configuration of annotation processors is needed for MapStruct to work with Lombok
© 2026 Devtiro Ltd. All rights reserved