MapStruct Configuration and Example in the Billing System

MapStruct Configuration and Example in the Billing System

This story explains how I have configured MapStruct in our Billing System Project.

The same article explained on YouTube. Please watch the full video:
https://youtu.be/LnSwxREmzXk

Add the following Artifact in the pom.xml file
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
In the Plugin section you have to following in the pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!– or newer version –>
<configuration>
<source>8</source> <!– depending on your project –>
<target>8</target> <!– depending on your project –>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

In this project, we are using the Lombok library for generating Getter, Setter, and Constructors. If you use Mapstruct in the Project, Lombok stops working in the Project. I was spent more time finding the root cause. Some conflict is happening between Lombok and Mapstruct for Annotation Processing. So, you have to add the following Lombok artifact part of the Mapstruct plugin.
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>

Once you added this to the plugin section, you good to go with Lombok and Mapstruct. Make sure whatever version you are using in the plugin section, better to use the same version of Lombok artifact in the dependency section, please check your Spring boot Lombok version otherwise override with this Lombok version. Please take a look at the GitHub. https://github.com/sureshstalin/billingsystem

Now MapStruct configuration is done. I am taking the example as UserDTO to User entity conversion from our code.
Step1
Since we need to convert UserDTO to User and User to UserDTO, we need to create a Mapper interface where we need to define the mapping configuration for UserDTO and User. Following is simple code I have done for conversion

@Mapper(implementationPackage = “mapper.impl”)
public interface UserMapper {

UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
User userDTOtoUser(UserDto userDto);
@InheritInverseConfiguration
UserDto userToUserDTO(User user);
}

We need to use @Mapper Annotation in our interface to achieve the mapping. In the first line, I am creating a UserMapper instance. Second-line I am defining a method called “userDTOtoUser(UserDto userDto)” and it accepts the UserDTO as a parameter. In the 3rd line, I am using an Annotation called “@InheritInverseConfiguration”. This is for opposite conversion. i.e User to UserDTO. That is it. My conversation code is done, Rest will take care of the MapperStruct framework. When you build the project, the “UserMappperImpl” Java class will be generated for mapping implementation for the method which we declared in the UserMapper Interface. This class will generate in Target Folder as in the below screenshot.

Mapstruct
MapStruct

In the @Mapper annotation parameter, I have passed the “implementation package” as “mapper.impl” so, the “UserMapperImpl” Java class created under mapper.impl package. The same package and Java class I have copied in the following location of source code.
MapStruct

Under Mapper Package I have UserMapper Interface and its implementation class available in “impl” Package. To test this mapper part, I have created a Small TestController class and it’s source code below.

@RestController
@RequestMapping("api/public/test")
public class TestController {

@PostMapping(“/mapper”)
public ResponseEntity<Void> mapperTesting(@RequestBody UserDto userDto) {
User user = UserMapper.INSTANCE.userDTOtoUser(userDto);
System.out.println(“Entity Class……”);
System.out.println(“Entity First Name ” + user.getFirstName());
System.out.println(“Entity Last Name ” + user.getLastName());
UserDto userDto2 = UserMapper.INSTANCE.userToUserDTO(user);
System.out.println(“DTO Class……”);
System.out.println(“DTO First Name ” + userDto2.getFirstName());
System.out.println(“DTO Last Name ” + userDto2.getLastName());
return new ResponseEntity<Void>(HttpStatus.OK);
}
}

JSON Request for above endpoint:
{
"firstName": "Suresh1",
"middleName":"test",
"lastName": "Stalin",
"mobileNo": "987652553",
"emailId": "suresh@gmail.com",
"flowType": "employee",
"roles":[
{
"name":"Guru",
"description":"This is Guru Role"
}
],
"addressList": [
{
"address1": "Ram Street",
"address2": "1st Lane",
"city": "Trichy",
"state": "TN",
"country": "India",
"landmark": "far",
"mobile": "343171771"
}
]
}

Output for above mapper code
Mapstruct

Lombok example code available @ following GitHub Repository
https://github.com/sureshstalin/itgarden/tree/master/lombok-mapstruct

Billing System updated code with Lombok and MapStruct.
https://github.com/sureshstalin/billingsystem

How to setup MapStruct in Spring Boot application while using Lombok Library
https://youtu.be/LnSwxREmzXk

Billing System-related videos available in the following Playlist ITGARDEN
https://www.youtube.com/playlist?list=PL2c68lJ7S5MouN0zUlNsOhYugkZTaZBsJ

 


Close Menu
×
×

Cart