Spring Boot Map One Object to Another




Hey guys in this short article, you will learn about Mapping one java object to another in Spring Boot.

Read More:

In order map one java object to another there so many libraries that are available, one of the popular library is ModelMapper.

Development Steps


In order to map the one java object to another object follow the below development steps

Add the maven dependency


First step is to add the Model mapper dependency

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

Autowire the Model mapper


Next step is to Autowire the Model mapper with @Bean annotation

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

Use Model mapper


Last step is the use the model mapper to convert the one object to another object

private Employee mapToEntity(EmployeeDto employeeDto) {
    Employee employee = modelMapper.map(employeeDto, Employee.class);
    return employee;
}

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

This Post Has One Comment

Leave a Reply