0
0
Spring Bootframework~5 mins

Response DTO for output in Spring Boot

Choose your learning style9 modes available
Introduction

A Response DTO helps send only the needed data back to users in a clear and simple way.

When you want to send specific data from your server to the client without exposing internal details.
When you want to format the output data differently from how it is stored in the database.
When you want to improve security by hiding sensitive fields from the response.
When you want to keep your API responses consistent and easy to understand.
When you want to add extra information like status messages or timestamps to the response.
Syntax
Spring Boot
public class ResponseDto {
    private String field1;
    private int field2;

    // Constructor
    public ResponseDto(String field1, int field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    // Getters and setters
    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public int getField2() {
        return field2;
    }

    public void setField2(int field2) {
        this.field2 = field2;
    }
}

A Response DTO is a simple Java class with private fields and public getters/setters.

It usually matches the data you want to send back, not the full database entity.

Examples
This DTO sends only username and email to the client, hiding other user details.
Spring Boot
public class UserResponseDto {
    private String username;
    private String email;

    public UserResponseDto(String username, String email) {
        this.username = username;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public String getEmail() {
        return email;
    }
}
Using Java record (Java 17+) makes a simple immutable DTO with less code.
Spring Boot
public record ProductResponseDto(String name, double price) {}
Sample Program

This Spring Boot example shows a Response DTO used in a controller to send a greeting message as JSON.

Spring Boot
package com.example.demo.dto;

public class GreetingResponseDto {
    private String message;

    public GreetingResponseDto(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

package com.example.demo.controller;

import com.example.demo.dto.GreetingResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public GreetingResponseDto greet() {
        return new GreetingResponseDto("Hello, welcome to our API!");
    }
}
OutputSuccess
Important Notes

Always keep Response DTOs simple and focused on what the client needs.

Use records in Java 17+ for concise immutable DTOs.

Do not expose sensitive or internal fields in your Response DTO.

Summary

Response DTOs shape the data sent back to clients clearly and safely.

They help separate internal data from what the user sees.

Using Response DTOs improves API design and security.