0
0
Spring Bootframework~20 mins

Response DTO for output in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response DTO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot Response DTO?
Given this simple Response DTO class and controller method, what JSON will be returned when the endpoint is called?
Spring Boot
public record UserResponseDTO(String name, int age) {}

@RestController
public class UserController {
    @GetMapping("/user")
    public UserResponseDTO getUser() {
        return new UserResponseDTO("Alice", 30);
    }
}
A{"name":"Alice","age":30}
B{"name":"Alice","age":"30"}
C{"name":"Alice"}
D{"age":30}
Attempts:
2 left
💡 Hint
Remember that Java records serialize all components by default as JSON properties.
📝 Syntax
intermediate
2:00remaining
Which Response DTO code snippet will cause a compilation error?
Identify the code snippet that will NOT compile as a valid Spring Boot Response DTO.
Apublic record ProductResponseDTO(String id, double price) { public ProductResponseDTO() {} }
Bpublic class ProductResponseDTO { private String id; private double price; public ProductResponseDTO(String id, double price) { this.id = id; this.price = price; } public String getId() { return id; } public double getPrice() { return price; } }
Cpublic record ProductResponseDTO(String id, double price) {}
Dpublic class ProductResponseDTO { public String id; public double price; }
Attempts:
2 left
💡 Hint
Records cannot have explicit no-argument constructors.
state_output
advanced
2:00remaining
What is the JSON output when a Response DTO has a nested object?
Consider this Response DTO with a nested Address record. What JSON is returned?
Spring Boot
public record Address(String street, String city) {}
public record UserResponseDTO(String name, Address address) {}

@RestController
public class UserController {
    @GetMapping("/user")
    public UserResponseDTO getUser() {
        Address addr = new Address("123 Main St", "Springfield");
        return new UserResponseDTO("Bob", addr);
    }
}
A{"name":"Bob","address":"123 Main St, Springfield"}
B{"name":"Bob","address":{"street":"123 Main St","city":"Springfield"}}
C{"name":"Bob","address":null}
D{"name":"Bob","address":{"street":"123 Main St"}}
Attempts:
2 left
💡 Hint
Nested records are serialized as nested JSON objects by default.
🔧 Debug
advanced
2:00remaining
Why does this Response DTO return an empty JSON object?
Given this DTO and controller, why does the API return {} instead of the expected fields?
Spring Boot
public class EmptyResponseDTO {
    private String data;

    public String getData() {
        return data;
    }
}

@RestController
public class TestController {
    @GetMapping("/test")
    public EmptyResponseDTO getTest() {
        EmptyResponseDTO dto = new EmptyResponseDTO();
        dto.data = "Hello";
        return dto;
    }
}
ABecause the field 'data' is not initialized in the constructor.
BBecause the DTO class is missing the @JsonProperty annotation on the field.
CBecause the controller method returns null instead of the DTO.
DBecause the field 'data' is private and has no getter method, Jackson cannot serialize it.
Attempts:
2 left
💡 Hint
Jackson needs public getters or annotations to serialize private fields.
🧠 Conceptual
expert
2:00remaining
Which statement about Response DTOs in Spring Boot is TRUE?
Select the one correct statement about how Response DTOs work in Spring Boot.
AResponse DTOs must always be immutable classes to be serialized correctly by Spring Boot.
BResponse DTOs require explicit @ResponseBody annotation on every field to be included in the JSON output.
CSpring Boot automatically converts Response DTOs to JSON using Jackson if the controller method returns an object and the client accepts JSON.
DSpring Boot only supports Response DTOs implemented as Java records, not regular classes.
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles controller return values and content negotiation.