Seamless Data Transformation in Spring Boot: Mastering Object Mapping with MapStruct

Anushka Muthusinghe
2 min readJun 30, 2024

In modern software development, mapping between different object models is a common task. This is especially true in Spring Boot applications where you often need to convert between entities and DTOs. Manually writing these mappings can be error-prone and tedious. This is where MapStruct comes into play. MapStruct is a Java annotation processor for generating type-safe, performant, and dependency-free bean mapping code.

What is MapStruct?

MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention-over-configuration approach. It leverages the power of annotations to automate the mapping process and generate the boilerplate code required for mapping objects.

Setting Up MapStruct in Spring Boot

To get started with MapStruct in a Spring Boot project, follow these steps:

Step 1: Add Dependencies

First, you need to add the MapStruct dependencies to your pom.xml file if you're using Maven:

<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.3.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

If you’re using Gradle, add the following to your build.gradle file:

dependencies {
implementation 'org.mapstruct:mapstruct:1.5.3.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'
}

Step 2: Create Your Entities and DTOs

For demonstration purposes, let’s create a simple User entity and a corresponding UserDTO.

// User entity
public class User {
private Long id;
private String firstName;
private String lastName;
private String email;

// Getters and setters
}

// UserDTO
public class UserDTO {
private Long id;
private String firstName;
private String lastName;

// Getters and setters
}jj

Step 3: Define a Mapper Interface

Create a mapper interface to define the mapping methods between User and UserDTO.

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

UserDTO userToUserDTO(User user);

User userDTOToUser(UserDTO userDTO);
}

Step 4: Using the Mapper in Your Service

You can now use the UserMapper in your service class to convert between User and UserDTO.

import org.springframework.stereotype.Service;

@Service
public class UserService {
public UserDTO getUserDTO(User user) {
return UserMapper.INSTANCE.userToUserDTO(user);
}

public User getUser(UserDTO userDTO) {
return UserMapper.INSTANCE.userDTOToUser(userDTO);
}
}

Conclusion

MapStruct simplifies object mapping in Spring Boot applications by generating the necessary boilerplate code, making your code cleaner and less error-prone. By following the steps outlined in this guide, you can quickly integrate MapStruct into your Spring Boot project and take advantage of its powerful mapping capabilities.

Stay tuned for our next article where we will dive into advanced mapping techniques with MapStruct. We will explore custom mapping logic, nested mappings, updating existing instances, and handling complex mapping scenarios to further enhance your Spring Boot applications.

Additional Resources

--

--