Spring boot : Mapstruct & Lombok

Fahd Boudali
5 min readNov 24, 2020

--

The Strike Team

Introduction

The aim of this article, is to get familiar with the use of Lombok & Mapstruct, they are two dependencies that you will import into your project, and they will save you valuable time.

I can’t be objective about using them or not, because I can no longer live without them (Okay, that was exaggerated).

The strike team from TV show : The shield. If you didn’t watch this, I can’t help
The shield : The strike team

The strike team is going to be : You, your IDE, Mapstruct and Lombok.

The project

Requirements to get your project up and running :

For this project, we are going to skip the use of a database, the model is going to be just POJO with no link to any database whatsoever.

Bootstrap the project :

First step, generating the project from : https://start.spring.io/

like following :

You can either generate the project or get it directly from GitHub.

Note that Lombok can be selected as a dependency from the initializer.

Setting up

In the root of your project, go to pom.xml file and add Mapstruct dependency

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
</dependency>

Then in the Build tag : <build>, we are going to configure maven compiler plugin to process Mapstruct annotations :

In order to simplify things, delete the src/main/test directory

You can already build your project :

mvn clean compile

The result must be :

Build success

Additional information :

if you are using IntelliJ IDEA, please do activate Annotation processing

Build > Execution > Deployment > Compiler > Annotation processing

Creating POJOs

POJO stands for Plain Old Java Object, to make it simple a POJO is a simple Java Object that has no logic in it and does not do any kind of operation.

That’s all you need to survive the Apocalypse.

Lombok :

For Lombok, the dependency should already be present in the pom.xml, since we have included

Let’s create :

  • Package : com.fboudali.training.springbootlombokmapstruct.domain
  • a Class :

Student.java

As you can see, fields are gray, the IDE tells us that the fields are not used….yet.

We used to generate Constructors, Getters and Setters for fields, it has been made simpler

Let’s add up some annotations to make our POJO more useable.

Student.java (Lomboked)

To see what Lombok did (generated) you can either list the class members or go to :

To see what Lombok did (generated) you can either list the class members or go to :

target
classes
com
fboudali
training
springbootlombokmapstruct
domain
Student.java members

@Data annotation :

I have seen a lot of developers using @Data along with their POJO,but it is an annotation that I personally don’t like that much.

Why ?

By taking a glance its documentation, you can see :

@Data javadoc

The annotation is an alias for several ones, and amongst these, we find
@EqualsAndHashCode, that means for each POJO holding this, hashcode and equals methods are going to be generated.
Adding the fact that we can see @ToString in it as well, I personally (again) prefer to set it manually in each POJO, so I can parametrize it the way I want.

We will be discussing Lombok more in depth in the future.

The full documentation can be found here

Mapstruct : Basics

We have already set up Mapstruct, by adding its dependency at the beginning, let’s try it out.

Mapstruct is a tool that helps (A LOT!) to map objects between the different layers you API can use, like mapping domain classes from the repository layer to DTOs (Data transfer object) for the controller through services.

So, create a new class StudentDto, in a new package : dto

This class has two fields, that are the same as Student.java’s, the mapping will be “natural” :

We need to fire this command again so maven could generate classes for the mapping

mvn clean compile

We can now see what has been generated, again either by opening the target directory at : com.fboudali.training.springbootlombokmapstruct.mapper

Or as following :

Opening Implementation for Student mapper

The result is :

Let’s test it :

Start the application

The result is shown to the console :



2020–11–24 14:47:28.709 INFO 21976 — — [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set ‘spring.devtools.add-properties’ to ‘false’ to disable
Fahd
BOUDALI

2020–11–24 14:47:29.313 INFO 21976 — — [ restartedMain] o.s.b.d.a.OptionalLiveReload….

```

Et Voilà !

Conclusion

This was a gentle introduction to how you can use Lombok and Mapstruct, and like you see, Spring boot helps a lot to get your project up and running quite fast.

The full project is on GitHub.

Next, Lombok and mapstruct in a real world application.

Stay tuned.

References:

Lombok official documentation :
https://projectlombok.org/features/all

Mapstruct official documentation : https://mapstruct.org/documentation/stable/reference/html/

Spring initializer
https://start.spring.io/

--

--

Fahd Boudali

Passionate about (quality) IT development, Dad & teacher & Lead developer. So … Follow my lead