0
0
Spring Bootframework~5 mins

DTO pattern for data transfer in Spring Boot

Choose your learning style9 modes available
Introduction

The DTO pattern helps move data between parts of an app clearly and simply. It keeps data organized and safe when sending it around.

When sending data from the server to the client in a web app.
When you want to hide some internal details of your data model.
When you need to combine data from different sources into one object.
When you want to validate or format data before sending it out.
When you want to reduce the amount of data sent over the network.
Syntax
Spring Boot
public class UserDTO {
    private String name;
    private String email;

    // Constructor
    public UserDTO(String name, String email) {
        this.name = name;
        this.email = email;
    }

    // Getters and setters
    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

A DTO is a simple class with only fields and getters/setters.

It does not contain business logic or database code.

Examples
Using Java 17+ record to create a simple immutable DTO.
Spring Boot
public record UserDTO(String name, String email) {}
A DTO for product data with name and price fields.
Spring Boot
public class ProductDTO {
    private String productName;
    private double price;

    // Constructors, getters, setters
}
Sample Program

This example shows a User model with sensitive data like password. The UserDTO only carries name and email to safely transfer data without exposing the password.

Spring Boot
package com.example.demo.dto;

public record UserDTO(String name, String email) {}

package com.example.demo.model;

public class User {
    private String name;
    private String email;
    private String password; // sensitive data

    public User(String name, String email, String password) {
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public String getName() { return name; }
    public String getEmail() { return email; }
    public String getPassword() { return password; }
}

package com.example.demo.service;

import com.example.demo.dto.UserDTO;
import com.example.demo.model.User;

public class UserService {
    public UserDTO convertToDTO(User user) {
        return new UserDTO(user.getName(), user.getEmail());
    }
}

package com.example.demo;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import com.example.demo.dto.UserDTO;

public class DemoApplication {
    public static void main(String[] args) {
        User user = new User("Alice", "alice@example.com", "secret123");
        UserService service = new UserService();
        UserDTO dto = service.convertToDTO(user);
        System.out.println("Name: " + dto.name());
        System.out.println("Email: " + dto.email());
    }
}
OutputSuccess
Important Notes

DTOs help keep your app secure by not exposing sensitive fields.

They make your API responses cleaner and easier to maintain.

Use Java records for simple, immutable DTOs if your Java version supports it.

Summary

DTOs are simple objects to move data safely between app parts.

They hide sensitive or unnecessary data from outside users.

Using DTOs makes your code cleaner and easier to change later.