Challenge - 5 Problems
Nested DTO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this nested DTO serialization?
Given the following Spring Boot DTO classes and a controller returning a nested DTO, what JSON will be returned to the client?
Spring Boot
public record AddressDTO(String street, String city) {}
public record UserDTO(String name, AddressDTO address) {}
@RestController
public class UserController {
@GetMapping("/user")
public UserDTO getUser() {
AddressDTO address = new AddressDTO("123 Main St", "Springfield");
return new UserDTO("John Doe", address);
}
}Attempts:
2 left
💡 Hint
Think about how nested records are serialized by default in Spring Boot with Jackson.
✗ Incorrect
The nested DTO AddressDTO is serialized as a JSON object inside the address field. So the full nested structure appears in the JSON response.
❓ state_output
intermediate1:30remaining
What is the value of the nested DTO field after mapping?
Consider these DTOs and a mapping method that creates a nested DTO. What is the value of
userDTO.address.city after calling mapToUserDTO()?Spring Boot
public record AddressDTO(String street, String city) {}
public record UserDTO(String name, AddressDTO address) {}
public UserDTO mapToUserDTO() {
AddressDTO address = new AddressDTO("456 Elm St", "Metropolis");
return new UserDTO("Alice", address);
}Attempts:
2 left
💡 Hint
Remember the nested DTO stores city as a separate field.
✗ Incorrect
The address field inside UserDTO holds an AddressDTO with city "Metropolis".
📝 Syntax
advanced2:30remaining
Which option correctly defines a nested DTO with Lombok in Spring Boot?
You want to create nested DTOs using Lombok annotations. Which code snippet correctly defines
UserDTO with a nested AddressDTO?Attempts:
2 left
💡 Hint
Nested DTOs require matching fields in both classes.
✗ Incorrect
Option A correctly defines both DTOs with all needed fields. Other options miss fields or mismatch types.
🔧 Debug
advanced3:00remaining
Why does this nested DTO cause a JSON serialization error?
Given these DTOs, the controller returns
UserDTO but the JSON response fails. What is the cause?Spring Boot
public class AddressDTO { private String street; private String city; // no getters or setters } public class UserDTO { private String name; private AddressDTO address; // getters and setters only for name } @RestController public class UserController { @GetMapping("/user") public UserDTO getUser() { AddressDTO address = new AddressDTO(); address.street = "789 Oak St"; address.city = "Gotham"; UserDTO user = new UserDTO(); user.setName("Bruce"); user.setAddress(address); return user; } }
Attempts:
2 left
💡 Hint
Jackson needs getters to read private fields during serialization.
✗ Incorrect
Jackson uses getters to serialize fields. Without getters in AddressDTO, it cannot read the nested fields, causing serialization failure.
🧠 Conceptual
expert3:30remaining
How does Spring Boot handle nested DTOs with circular references during JSON serialization?
Consider two nested DTOs referencing each other, causing a circular reference. What is the default behavior of Spring Boot's JSON serialization, and how can it be resolved?
Attempts:
2 left
💡 Hint
Think about how Jackson handles circular references by default.
✗ Incorrect
By default, Jackson (used by Spring Boot) throws a StackOverflowError on circular references. Using annotations like @JsonManagedReference and @JsonBackReference helps break the cycle during serialization.