Member-only story
Using MapStruct within Quarkus
Mapping from entities to DTOs and vice versa is always required within multi-layered applications for transferring data between services/processes. How these mappings is implemented is also another important topic from development perspective as it can have impacts on performance or even on code quality & code readability.
MapStruct is a powerful processor which can map between beans and which can create concrete implementation methods during compile time. I will mention about using MapStruct for mapping MongoDB entities to DTOs and vice versa within Quarkus in this article.
Also, I am sharing the all code base from my GitHub account.
A Brief Intro to MapStruct
MapStruct implements bean mappings during compile time which provides a high performance and makes life easier for developers as mappings can be applied with configuration approach. You can increase the code readability & decrease the complexity with using annotations provided within MapStruct library.
Mapping methods can be implemented within interface or abstract classes and MapStruct generates the implementation class automatically according to our declared methods.
For more information, below link from mapstruct homepage can be checked.
Using MapStruct with MongoDB Entities
I will continue with the example from my previous article, MongoDB implementation in Quarkus. It will be good to check that one before going into next steps.
Creating DTO
So, let’s first create our DTO, it will have the same attributes as our EnterpiseEntity.