Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nested DTOs in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage books and their authors. Each book has a title and an author. The author has a name and an email.
🎯 Goal: Create nested DTO (Data Transfer Object) classes to represent a BookDTO that contains an AuthorDTO. This helps organize data cleanly when sending or receiving JSON in your application.
📋 What You'll Learn
Create a class AuthorDTO with fields name and email.
Create a class BookDTO with fields title and author of type AuthorDTO.
Use Java records to define these DTOs for simplicity.
Ensure the nested structure is correct for JSON serialization.
💡 Why This Matters
🌍 Real World
Nested DTOs are common in web APIs where objects contain other objects, like a book containing an author. This helps keep data organized and easy to manage.
💼 Career
Understanding nested DTOs is essential for backend developers working with Spring Boot to build clean, maintainable APIs that handle complex data structures.
Progress0 / 4 steps
1
Create the AuthorDTO record
Create a Java record called AuthorDTO with two fields: String name and String email.
Spring Boot
Hint
Use the record keyword to create a simple immutable data class with the specified fields.
2
Create the BookDTO record with nested AuthorDTO
Create a Java record called BookDTO with two fields: String title and AuthorDTO author. Use the previously created AuthorDTO as the type for the author field.
Spring Boot
Hint
Use the AuthorDTO type inside BookDTO to nest the author information.
3
Create an instance of AuthorDTO
Create a variable called author and assign it a new AuthorDTO instance with name set to "Jane Austen" and email set to "jane.austen@example.com".
Spring Boot
Hint
Use the new keyword to create an instance of the AuthorDTO record with the exact values.
4
Create an instance of BookDTO using the nested AuthorDTO
Create a variable called book and assign it a new BookDTO instance with title set to "Pride and Prejudice" and author set to the previously created author variable.
Spring Boot
Hint
Use the author variable as the second argument when creating the BookDTO instance.
Practice
(1/5)
1. What is the main purpose of using Nested DTOs in Spring Boot applications?
easy
A. To handle HTTP requests without controllers
B. To group related data inside other data objects for better structure
C. To replace entity classes with simpler objects
D. To improve database query performance automatically
Solution
Step 1: Understand DTO role
DTOs (Data Transfer Objects) are used to carry data between processes or layers.
Step 2: Identify Nested DTO purpose
Nested DTOs group related data inside other DTOs to represent complex data structures clearly.
Final Answer:
To group related data inside other data objects for better structure -> Option B
Quick Check:
Nested DTOs = Group related data [OK]
Hint: Nested DTOs organize data inside other objects [OK]
Common Mistakes:
Thinking nested DTOs improve database speed
Confusing DTOs with entities
Assuming nested DTOs replace controllers
2. Which of the following is the correct way to declare a nested DTO class inside a parent DTO in Spring Boot?
easy
A. public class ParentDTO { private class ChildDTO { private String name; } }
B. public class ParentDTO { class ChildDTO { private String name; } }
C. public class ParentDTO { public static class ChildDTO { private String name; } }
D. public class ParentDTO { static class ChildDTO { public String name; } }
Solution
Step 1: Check nested class modifiers
Static nested classes are recommended for DTOs to avoid implicit reference to outer class.
Step 2: Validate access modifiers
Public static nested class with private fields and getters/setters is standard practice.
Final Answer:
public class ParentDTO { public static class ChildDTO { private String name; } } -> Option C
Quick Check:
Static nested class with public modifier = public class ParentDTO { public static class ChildDTO { private String name; } } [OK]
Hint: Use public static nested class for nested DTOs [OK]
Common Mistakes:
Using non-static nested classes causing memory leaks
Declaring nested class as private making it inaccessible
Using public fields instead of private with getters/setters
3. Given the following nested DTO classes, what will be the output of System.out.println(order.getCustomer().getName()); if order is initialized as below?
public class OrderDTO {
private CustomerDTO customer;
public CustomerDTO getCustomer() { return customer; }
public void setCustomer(CustomerDTO customer) { this.customer = customer; }
public static class CustomerDTO {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
}
OrderDTO order = new OrderDTO();
OrderDTO.CustomerDTO cust = new OrderDTO.CustomerDTO();
cust.setName("Alice");
order.setCustomer(cust);
medium
A. Alice
B. null
C. Compilation error
D. Runtime NullPointerException
Solution
Step 1: Analyze object initialization
The customer object is created and its name is set to "Alice" before being assigned to order.
Step 2: Check method calls
Calling order.getCustomer().getName() returns the name "Alice" as set previously.
Final Answer:
Alice -> Option A
Quick Check:
Nested DTO getter returns set value = Alice [OK]
Hint: Set nested DTO fields before accessing getters [OK]
Common Mistakes:
Forgetting to set nested DTO before calling getter
Confusing null with empty string
Assuming compilation error due to nested class
4. Identify the error in the following nested DTO code snippet:
public class UserDTO {
private AddressDTO address;
public static class AddressDTO {
private String city;
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
}
public AddressDTO getAddress() { return address; }
public void setAddress(AddressDTO address) { this.address = address; }
}
UserDTO user = new UserDTO();
user.getAddress().setCity("Paris");
medium
A. Compilation error due to missing constructor
B. No error, code runs correctly
C. IllegalAccessError on accessing city field
D. NullPointerException because address is not initialized
Solution
Step 1: Check object initialization
The address field in UserDTO is never initialized, so it is null by default.
Step 2: Analyze method call
Calling user.getAddress().setCity("Paris") tries to call setCity on null, causing NullPointerException.
Final Answer:
NullPointerException because address is not initialized -> Option D
5. You have a nested DTO structure where OrderDTO contains a list of ItemDTO objects. You want to convert this nested DTO into a flat list of item names using Java streams. Which code snippet correctly achieves this?
public class OrderDTO {
private List<ItemDTO> items;
public List<ItemDTO> getItems() { return items; }
public void setItems(List<ItemDTO> items) { this.items = items; }
public static class ItemDTO {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
}
OrderDTO order = ...; // initialized with items
hard
A. List<String> names = order.getItems().stream().map(OrderDTO.ItemDTO::getName).toList();
B. List<String> names = order.getItems().stream().flatMap(ItemDTO::getName).collect(Collectors.toList());
C. List<String> names = order.getItems().map(ItemDTO::getName).collect(Collectors.toList());
D. List<String> names = order.getItems().stream().map(item -> item.name).collect(Collectors.toList());
Solution
Step 1: Understand stream mapping
To get a list of names, map each ItemDTO to its name using map(OrderDTO.ItemDTO::getName).
Step 2: Collect results
Use toList() (Java 16+) or collect(Collectors.toList()) to gather results into a list.
Final Answer:
List<String> names = order.getItems().stream().map(OrderDTO.ItemDTO::getName).toList(); -> Option A