0
0
Spring Bootframework~5 mins

Entity to DTO mapping in Spring Boot

Choose your learning style9 modes available
Introduction

Entity to DTO mapping helps separate how data is stored from how it is sent or shown. It keeps your app clean and safe.

When you want to send only some data from your database to the user.
When you want to hide sensitive information like passwords from being sent out.
When you want to change the data format before sending it to the user.
When you want to reduce the amount of data sent over the network.
When you want to keep your database structure separate from your app's public interface.
Syntax
Spring Boot
public class Entity {
    private Long id;
    private String name;
    private String secret;
    // getters and setters
}

public class EntityDTO {
    private Long id;
    private String name;
    // getters and setters
}

// Mapping method example
public EntityDTO toDTO(Entity entity) {
    EntityDTO dto = new EntityDTO();
    dto.setId(entity.getId());
    dto.setName(entity.getName());
    return dto;
}

DTO stands for Data Transfer Object, a simple object to carry data.

Mapping means copying data from Entity to DTO manually or using tools.

Examples
Simple manual mapping copying only needed fields.
Spring Boot
public EntityDTO toDTO(Entity entity) {
    EntityDTO dto = new EntityDTO();
    dto.setId(entity.getId());
    dto.setName(entity.getName());
    return dto;
}
Using MapStruct library to generate mapping code automatically.
Spring Boot
@Mapper
public interface EntityMapper {
    EntityDTO toDTO(Entity entity);
}
Using ModelMapper library to map Entity to DTO in one line.
Spring Boot
EntityDTO dto = modelMapper.map(entity, EntityDTO.class);
Sample Program

This Spring Boot app has a UserEntity with sensitive password. The controller sends UserDTO without password to keep data safe.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

class UserEntity {
    private Long id;
    private String username;
    private String password; // sensitive

    public UserEntity(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Long getId() { return id; }
    public String getUsername() { return username; }
    public String getPassword() { return password; }
}

class UserDTO {
    private Long id;
    private String username;

    public UserDTO(Long id, String username) {
        this.id = id;
        this.username = username;
    }

    public Long getId() { return id; }
    public String getUsername() { return username; }
}

@RestController
class UserController {

    @GetMapping("/user")
    public UserDTO getUser() {
        UserEntity userEntity = new UserEntity(1L, "alice", "secret123");
        return toDTO(userEntity);
    }

    private UserDTO toDTO(UserEntity entity) {
        return new UserDTO(entity.getId(), entity.getUsername());
    }
}
OutputSuccess
Important Notes

Always exclude sensitive or unnecessary fields in DTOs.

Manual mapping is simple but can be repetitive; libraries help automate it.

Keep DTOs simple and focused on what the client needs.

Summary

Entity to DTO mapping separates database data from what you send outside.

It helps protect sensitive info and control data format.

You can map manually or use libraries like MapStruct or ModelMapper.