Hey guys in this short article, you will learn about Mapping one java object to another in Spring Boot.
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
In order map one java object to another there so many libraries that are available, one of the popular library is ModelMapper.
Table of Contents
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.
Pingback: How do I map one model to another in spring boot? – Jiuya Tech Blog