VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-boot-map-entity-to-dto-using-modelmapper/

⇱ Spring Boot - Map Entity to DTO using ModelMapper - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot - Map Entity to DTO using ModelMapper

Last Updated : 29 Apr, 2026

In Spring Boot, ModelMapper is a library used to automatically map data between Entity objects and DTO (Data Transfer Object) classes. It simplifies the process of converting objects by reducing the need for manual mapping code. This helps keep the application clean, maintainable, and secure by controlling the data sent between layers.

  • Supports custom mappings and configuration for complex object transformations.
  • Improves performance and readability by avoiding repetitive setter/getter logic.
  • Enhances data security by exposing only required fields through DTOs.

DTO

A DTO (Data Transfer Object) is a simple Java class used to transfer data between layers of an application without exposing the internal entity structure.

  • Helps hide sensitive fields (e.g., password) from being exposed in API responses.
  • Allows developers to selectively expose only required data to the web layer.

Syntax:

Step By Step Implementation

Step 1: Create Spring Boot Project

Create a Spring Boot project using an IDE like Eclipse or IntelliJ.

  • Use Spring Initializr to generate the project.
  • Add required dependencies like Spring Web, Spring Data JPA, MySQL Driver .
  • Import the project into the IDE.
👁 Fig 1 - Add Dependency

Step 2: Add ModelMapper Dependency

Add the ModelMapper dependency in the pom.xml file to enable automatic mapping between Entity and DTO.

Syntax:

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

Step 3: Create Database Schema

Create a database schema in MySQL to store application data.

  • Open MySQL Workbench.
  • Go to Schemas -> Right Click -> Create Schema.
  • Enter schema name model_mapper_db and click Apply.
👁 Fig 2 - Create Schema
👁 Fig 3 - DB Schema Name

Once, we add the name of our schema, we need to click on Apply button, it will pop up one more window where we need to click again on Apply button. This will create our schema.

👁 Fig 4 - Database Schema

Step 4: Configure Database

Configure database connection properties in application.properties.

Syntax:

server.port=9090
# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/model_mapper_db
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Run the application to check whether the database is configured correctly. Go to the main class (ModelMapperApplication.java) and select Right Click -> Run As -> Java Application.

👁 Fig 5 - Run Application

Once, we run our application as Java Application, we can see in the console that our application is started and set up the connection to the database.

👁 Fig 6 - DB Connection Setup

Step 5: Create Entity Class

Create an Entity class representing the database table.
For our application, we will be using the User class as our entity class. It will have the following fields id, name, email, and password.

User.java

Once, we have written our entity class. We will run our application, As we have done the database configuration, JPA will create the User table in the database automatically using the annotation we have added to our entity class.

👁 Fig 7 - User Table

Step 6: Create a User Repository

Create an interface and name it as UserRepository and extends this class to the JPA repository. So, we can have CRUD operations easily available.

UserRepository.java

Step 7: Create User Service

Now, we will create a service interface and name it as UserService. We will add only two methods to it. One to add the user and the other to get the user.

UserService.java

Step 8: Create Service Interface

UserServiceImpl.java

Step 9: Create a Controller

In this step, we will create a user controller for handling and mapping our request.

UserController.java

Step 10: Run the Application

In this step, we will run our application and test restful services using postman.

1. CREATE A USER:

👁 Fig 8 - Create Request

Once, we send our request. We will get the following output.

👁 Fig 9 - Response

We can also check our database for new user entries.

👁 Fig 10 - User Added To DB

2. GET A USER:

We will be using the GET endpoint and userid to retrieve users from the database.

👁 Fig 11 - Get A User

As we can see in the above response, we will also receive passwords which is not a good practice to write restful APIs. To overcome this problem, we will use DTO.

Step 11: Create DTO

Create UserDTO class that will contain only those fields that are required and necessary for the web layer.

UserDto.java

Step 12: Adding Model Mapper Dependency

We need to add the following dependency in our pom.xml file.

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

Step 13: Modify Classes

Now, to use UserDto, we need to modify our UserService, UserServiceImpl, and UserController class.

UserService.java

UserServiceImpl.java

UserController.java

Step 14: Add Model Mapper Bean

In this step, we will add model mapper bean to our main spring boot class.

Step 15: Run the Application

Now, we will again run our application and use the GET endpoint to see the response.

👁 Fig 12 - Response

The response now contains only the required fields for the web layer. Although the User is created with all fields, only the necessary fields are returned in the response.

Comment