How to use ModelMapper in Spring Boot

Zubair Rehman
3 min readJan 28, 2023

--

Photo by Marjan Blan | @marjanblan on Unsplash

ModelMapper is a Java library that helps in mapping objects from one model to another, reducing the need for manual mapping code. It is a highly configurable library that uses conventions to automatically map objects, but can also be configured to provide custom mapping rules. In this article, we will demonstrate how to use ModelMapper in a Spring Boot application.

Integrating ModelMapper with Spring Boot

To use ModelMapper in a Spring Boot application, we first need to add it to our project’s dependencies. This can be done by adding the following to the pom.xml file for a Maven-based project:

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>

If you are using Gradle, you can add the following to your build.gradle file:

implementation 'org.modelmapper:modelmapper:2.3.8'

Next, we need to create a configuration class that will initialize ModelMapper and makes it available as a Bean in our application context.

@Configuration
public class ModelMapperConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}

Mapping Objects with ModelMapper

Now that we have ModelMapper set up in our application, we can start using it to map objects from one model to another. Let’s consider an example of a simple model class Person:

public class Person {
private String firstName;
private String lastName;
private int age;

// Getters and setters
}

And another model class PersonDTO:

public class PersonDTO {
private String fullName;
private int age;

// Getters and setters
}

We can map the Person object to a PersonDTO object using ModelMapper as follows:

@Autowired
private ModelMapper modelMapper;

Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(30);

PersonDTO personDTO = modelMapper.map(person, PersonDTO.class);

In this example, we use the map method of the ModelMapper instance to map the Person object to a PersonDTO object. ModelMapper uses conventions to automatically map the properties that have the same name in both classes. In this case, the firstName and lastName properties of the Person object is automatically mapped to the fullName property of the PersonDTO object.

Custom Mapping Rules

In some cases, we may need to provide custom mapping rules, such as mapping a property to multiple target properties or converting a property value before mapping it. ModelMapper provides several methods to configure custom mapping rules, including typeMap, addMappings, and validate.

Let’s consider an example of a custom mapping rule for mapping the firstName and lastName properties of the Person object to the fullName property of the PersonDTO.

By default, Model Mapper maps properties from the source object to the target object based on their names and types. In this scenario, the age property can be mapped automatically, but the fullName the property requires a custom mapping rule.

Here’s how we can define a custom mapping rule using Model Mapper:

ModelMapper modelMapper = new ModelMapper();
TypeMap<Person, PersonDTO> typeMap = modelMapper.createTypeMap(Person.class, PersonDTO.class);
typeMap.addMappings(mapper -> {
mapper.map(src -> src.getFirstName() + " " + src.getLastName(), PersonDTO::setFullName);
});

In the code above, we first create an instance of the ModelMapper class. Then we create a TypeMap using the createTypeMap method, passing in the Person and PersonDTO classes as arguments. Finally, we use the addMappings method to specify a custom mapping rule.

The custom mapping rule uses a lambda expression to concatenate the firstName and lastName properties of the Person object and set the resulting value to the fullName property of the PersonDTO object.

With the custom mapping rule in place, we can now map a Person object to a PersonDTO object:

Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(30);

PersonDTO personDTO = modelMapper.map(person, PersonDTO.class);

After mapping, the personDTO object would have the following values:

fullName = "John Doe"
age = 30

In conclusion, custom mapping rules in Model Mapper provide a way to specify how properties from one model should be mapped to properties in another model. This is especially useful when the default conventions don’t suffice.

Thats it for this article, if you liked this article, don’t forget to clap your hands 👏 as many times as you can to show your support, leave your comments and share it with your friends.

--

--