Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1. What is the main purpose of using a Response DTO in a Spring Boot application?
easy
A. To handle incoming HTTP requests
B. To define the exact data structure sent back to the client
C. To store data in the database
D. To configure application properties

Solution

  1. Step 1: Understand the role of Response DTO

    A Response DTO is used to shape the data sent back to the client, controlling what fields are exposed.
  2. Step 2: Differentiate from other components

    It is not used for storing data or handling requests, but specifically for output formatting.
  3. Final Answer:

    To define the exact data structure sent back to the client -> Option B
  4. Quick Check:

    Response DTO = Output data structure [OK]
Hint: Response DTO controls output data format [OK]
Common Mistakes:
  • Confusing Response DTO with entity or request DTO
  • Thinking Response DTO handles input data
  • Assuming Response DTO manages database operations
2. Which of the following is the correct way to define a simple Response DTO class in Spring Boot?
easy
A. public record UserResponse(String name) {}
B. public class UserResponse { private String name; public String getName() { return name; } }
C. public enum UserResponse { NAME; }
D. public interface UserResponse { String name; }

Solution

  1. Step 1: Identify valid Java class for DTO

    Spring Boot supports Java records as concise DTOs with immutable fields and automatic getters.
  2. Step 2: Check syntax correctness

    public record UserResponse(String name) {} uses a record with a field and no boilerplate code, which is modern and valid.
  3. Final Answer:

    public record UserResponse(String name) {} -> Option A
  4. Quick Check:

    Use record for simple immutable DTO [OK]
Hint: Use Java record for simple DTOs [OK]
Common Mistakes:
  • Using interface instead of class or record
  • Using enum for data container
  • Omitting getters in POJO classes
3. Given this Response DTO and controller method, what JSON will be returned?
public record ProductResponse(String name, double price) {}

@GetMapping("/product")
public ProductResponse getProduct() {
  return new ProductResponse("Book", 12.5);
}
medium
A. {"name":"Book"}
B. {"productName":"Book","productPrice":12.5}
C. {"name":"Book","price":12.5}
D. {"name":"Book","price":"12.5"}

Solution

  1. Step 1: Understand record fields and JSON mapping

    The record fields are name and price, which map directly to JSON keys.
  2. Step 2: Check returned JSON structure

    The returned JSON includes both fields with correct types: string for name and number for price.
  3. Final Answer:

    {"name":"Book","price":12.5} -> Option C
  4. Quick Check:

    Record fields map directly to JSON keys [OK]
Hint: Record fields become JSON keys as-is [OK]
Common Mistakes:
  • Assuming JSON keys change names automatically
  • Treating numbers as strings in JSON
  • Missing fields in output JSON
4. What is wrong with this Response DTO class?
public class UserResponse {
  private String username;
  public UserResponse(String username) {}
  public String getUsername() { return username; }
}
medium
A. Class should be abstract
B. Getter method is missing
C. Field username should be public
D. Constructor does not assign the username field

Solution

  1. Step 1: Check constructor implementation

    The constructor has a parameter but does not assign it to the field, so username remains null.
  2. Step 2: Verify getter correctness

    The getter returns the field value, but since field is never set, it returns null.
  3. Final Answer:

    Constructor does not assign the username field -> Option D
  4. Quick Check:

    Constructor must set fields [OK]
Hint: Always assign constructor params to fields [OK]
Common Mistakes:
  • Forgetting to assign constructor parameters
  • Making fields public unnecessarily
  • Thinking getters alone set values
5. You want to create a Response DTO that only exposes a user's id and email, hiding the password. Which approach is best in Spring Boot?
hard
A. Create a separate Response DTO class with only id and email fields
B. Return the User entity directly and ignore the password field
C. Use @JsonIgnore on the password field in the User entity
D. Send the entire User entity and filter password on the client side

Solution

  1. Step 1: Understand security and data exposure

    Exposing only needed fields via a Response DTO prevents accidental leaks of sensitive data like passwords.
  2. Step 2: Evaluate options for hiding password

    Creating a separate DTO with only safe fields is best practice; relying on @JsonIgnore or client filtering is less secure or less clear.
  3. Final Answer:

    Create a separate Response DTO class with only id and email fields -> Option A
  4. Quick Check:

    Separate DTOs protect sensitive data [OK]
Hint: Use dedicated DTOs to expose only safe fields [OK]
Common Mistakes:
  • Returning entity directly exposing sensitive data
  • Relying on client-side filtering for security
  • Misusing @JsonIgnore without DTO separation