Spring Boot DTO to Entity mapping




Hey guys in this article, you will learn about mapping the DTO to Entity class in Spring boot or Spring MVC.

Read More:

There are so many different ways to map the DTO to Entity or vice versa in spring boot, we can use Modelmapper, Mapstruct, Dozermapper.

Complete Example


Follow the complete article to learn about mapping the DTO to Entity object using Model mapper.

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies


Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.5</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>springbootapp</artifactId>
	<version>1.0.0</version>
	<name>springbootapp</name>
	<description>Spring boot app</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.modelmapper</groupId>
			<artifactId>modelmapper</artifactId>
			<version>0.7.4</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container. org.modelmapper dependency is for mapping the DTO to entity and entity to DTO using model mapper.



Create DTO class


Create EmployeeDTO.java inside in.bushansirgur.dto package and add the following content

package in.bushansirgur.springbootapp.entity;

public class EmployeeDTO {
	
private String name;
	
	private int age;
	
	private String location;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
	
}

Create Entity class


Create Employee.java inside in.bushansirgur.entity package and add the following content

package in.bushansirgur.springbootapp.entity;

public class Employee {
	
private String name;
	
	private int age;
	
	private String location;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", location=" + location + "]";
	}
	
}

Create a Controller


Create HomeController.java inside in.bushansirgur.controller package add the following content

package in.bushansirgur.springbootapp.controller;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springbootapp.entity.Employee;
import in.bushansirgur.springbootapp.model.EmployeeDTO;

@RestController
public class HomeController {
	
	@Autowired
	private ModelMapper mapper;
	
	@PostMapping("/save")
	public Employee save(@RequestBody EmployeeDTO employeeDto) {
		Employee employee = mapper.map(employeeDto, Employee.class);
		System.out.println("Printing employee after mapping:"+employee);
		return employee;
	}
}

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the browser and enter the following URL –

http://localhost:8080/save
05
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.



About the author

Bushan Sirgur

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

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *