0
0
Spring Bootframework~5 mins

Nested DTOs in Spring Boot

Choose your learning style9 modes available
Introduction

Nested DTOs help organize related data inside other data objects. They make it easier to send or receive complex information in a clear way.

When you want to send a user object that contains an address object inside it.
When your API needs to return a product with a list of reviews, each review being its own object.
When you want to group related fields together inside a bigger data transfer object.
When you want to keep your code clean by separating parts of data into smaller objects.
When you need to map complex JSON structures to Java objects in Spring Boot.
Syntax
Spring Boot
public class OuterDTO {
    private InnerDTO inner;

    // getters and setters

    public static class InnerDTO {
        private String field;

        // getters and setters
    }
}
Nested DTOs are usually static inner classes or separate classes referenced inside another DTO.
Use getters and setters or Lombok annotations to access nested fields.
Examples
This example shows a UserDTO containing an AddressDTO as a nested object.
Spring Boot
public class UserDTO {
    private String name;
    private AddressDTO address;

    // getters and setters
}

public class AddressDTO {
    private String street;
    private String city;

    // getters and setters
}
Here, OrderDTO has a list of ItemDTO objects to represent multiple items.
Spring Boot
import java.util.List;

public class OrderDTO {
    private int id;
    private List<ItemDTO> items;

    // getters and setters
}

public class ItemDTO {
    private String productName;
    private int quantity;

    // getters and setters
}
This shows a static nested DTO class inside another DTO class.
Spring Boot
public class OuterDTO {
    private InnerDTO inner;

    public static class InnerDTO {
        private String detail;

        // getters and setters
    }

    // getters and setters
}
Sample Program

This example defines a PersonDTO with a nested ContactDTO inside it. The main method creates a person with contact details and prints them.

Spring Boot
package com.example.demo.dto;

public class PersonDTO {
    private String name;
    private ContactDTO contact;

    public PersonDTO() {}

    public PersonDTO(String name, ContactDTO contact) {
        this.name = name;
        this.contact = contact;
    }

    public String getName() {
        return name;
    }

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

    public ContactDTO getContact() {
        return contact;
    }

    public void setContact(ContactDTO contact) {
        this.contact = contact;
    }

    public static class ContactDTO {
        private String email;
        private String phone;

        public ContactDTO() {}

        public ContactDTO(String email, String phone) {
            this.email = email;
            this.phone = phone;
        }

        public String getEmail() {
            return email;
        }

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

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
    }
}

// Usage example in a main method or test
class Main {
    public static void main(String[] args) {
        PersonDTO.ContactDTO contact = new PersonDTO.ContactDTO("alice@example.com", "123-456-7890");
        PersonDTO person = new PersonDTO("Alice", contact);

        System.out.println("Name: " + person.getName());
        System.out.println("Email: " + person.getContact().getEmail());
        System.out.println("Phone: " + person.getContact().getPhone());
    }
}
OutputSuccess
Important Notes

Nested DTOs help keep related data together and improve code organization.

Remember to provide getters and setters for nested objects to access their fields.

Using static nested classes avoids unnecessary references to the outer class instance.

Summary

Nested DTOs group related data inside other data objects.

They make it easier to handle complex data structures in Spring Boot.

Use static inner classes or separate classes to create nested DTOs.