{{announcement.body}}
{{announcement.title}}

Introduction to MapStruct: An Easy and Fast Mapping at Compile Time

DZone 's Guide to

Introduction to MapStruct: An Easy and Fast Mapping at Compile Time

MapStruct: easy mappings between Java beans

· Java Zone ·
Free Resource

In a previous article, we learned about the proposal of DTO, the benefits and the issues about this new layer in our Java Application We can isolate the model from control in the MVC architecture perspective, although we add a new layer that implies more complexity, furthermore,  there is a work of conversion between entity and DTO. In this tutorial, we'll explore one more tool the MapStruct.

MapStruct is a code generator that dramatically simplifies the mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses explicit method invocations and thus is fast, type-safe, and easy to understand.

To demonstrate how to use this mapper and compare the ModelMapper with MapStruct, we'll rewrite the same application, however, with MapStruct instead. Thus, we'll create a MicroProfile/Jakarta EE application with Payara Micro to store the user information; on this service, we'll store the nickname, salary, languages, birthday, and several settings.

We'll use a maven project, therefore, we need to add the MapStruct in your dependencies as the first step.

XML
 







In the persistence layer, we'll use MongoDB that is so far the most popular NoSQL database in the globe. To make the communication more natural between Java and the database, we'll use Jakarta NoSQL. We'll define default settings to run locally, but thanks to Eclipse MicroProfile, we can overwrite those on the production environment.

Java
 







The Magic happens in the interface that has the Mapper annotation, which marks the interface as a mapping interface and lets the MapStruct processor kick in during compilation. We set the component as CDI because we'll make the interface eligible to CDI context injection.

Java
 







One of the significant advantages of using Jakarta NoSQL is its ease of integrating the database. For example, in this article, we will use the concept of a repository from which we will create an interface for which Jakarta NoSQL will take care of this implementation.

Java
 







In the last step, we will make our appeal with JAX-RS. The critical point is that the data exposure will all be done from the DTO; that is, it is possible to carry out any modification within the entity without the customer knowing, thanks to the DTO. As mentioned, the mapper was injected, and the 'map' method greatly facilitates this integration between the DTO and the entity without much code for that.

Java
 







The application is ready to run locally; we have either the docker image or install manually option to run a MongoDB instance and have fun.

In this tutorial, we'll move our application in the next step, moving it to the cloud, thanks to Platform.sh, where we need three files:

1) One Yaml files that have the application descriptions, such as the language, language version, who to build an application, and finally, how to execute it.

YAML
 







2) One to determine the services that your application need, yeap, don't worry about how to handle and maintain it, Platform.sh handles that to us.

YAML
 







3) The last you explain the routes

YAML
 







It is easy, isn't it? MapStruct allows a unique interface to create an optimized mapper in the Java world. It does not use reflections, therefore, it won't have the same issues that brings with reflection use. Software Architecture is about to take and understand the trade-off in any kind of situation.

Topics:
cloud (add topic), dto, java, paas, platform.sh

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}