How to fix MapStruct in Spring Boot when return null object

Supakon_k
2 min readOct 16, 2021

--

The problem can happen when you use MapStruct with Lombok. If you don’t use Lombok in the project you can skip this article, but if you do, let’s try with us.

The problem is in the file pom.xml in the dependency section you may add MapStruct dependency on top of Lombok dependency you should switch MapStruct dependency to the bottom of Lombok dependency, in my case it works for example.

here is the code on GitHub: werterzz/fix-mapstruct-object-null-problem (github.com)

I created files following by this structure

User.java

UserDto.java

UserMapper.java

FixMapstructObjectNullProblemApplication.java

pom.xml (MapStruct dependency section) [wrong case]

When you use dependency with the wrong case (use MapStruct before Lombok) then start the server and get request, you will get object with null value.

pom.xml (MapStruct dependency section) [correct case]

When you use dependency with the correct case (use MapStruct after Lombok) then start the server and get request, you will get object with string value.

So, in my experiment, I think dependency order in pom.xml may have an effect on your code. Thanks for reading and enjoy your code.

buymeacoffee.com/werterzz

--

--