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
Response DTO for output
📖 Scenario: You are building a simple Spring Boot application that returns user information as a response. To keep your code clean and organized, you want to create a Response DTO (Data Transfer Object) that will hold the user data sent back to the client.
🎯 Goal: Create a Response DTO class in Spring Boot that holds user data fields and use it to return user information in a structured way.
📋 What You'll Learn
Create a Java class named UserResponseDto with private fields: id (Long), name (String), and email (String).
Add public getters and setters for all fields.
Add a constructor that accepts all fields as parameters.
Use the DTO class to prepare a response object.
💡 Why This Matters
🌍 Real World
Response DTOs are used in real applications to send only the necessary data to clients in a clean and organized way, improving API design and maintainability.
💼 Career
Understanding how to create and use Response DTOs is essential for backend developers working with Spring Boot to build RESTful APIs.
Progress0 / 4 steps
1
Create the Response DTO class
Create a public Java class named UserResponseDto with private fields id of type Long, name of type String, and email of type String.
Spring Boot
Hint
Define the class and declare the fields as private variables inside it.
2
Add constructor and getters/setters
Add a public constructor to UserResponseDto that accepts Long id, String name, and String email as parameters and assigns them to the fields. Also add public getter and setter methods for each field.
Spring Boot
Hint
Write a constructor that sets all fields. Then add getter and setter methods for each field.
3
Create a sample UserResponseDto object
In a separate class or method, create a new UserResponseDto object named response using the constructor with id as 1L, name as "Alice", and email as "alice@example.com".
Spring Boot
Hint
Create a new instance of UserResponseDto using the constructor with the exact values.
4
Use Response DTO in a Spring Boot controller method
Write a Spring Boot controller method named getUser that returns the UserResponseDto object response. The method should be annotated with @GetMapping("/user") and return type UserResponseDto.
Spring Boot
Hint
Create a controller class with a method annotated with @GetMapping("/user") that returns the response object.
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
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.
Step 2: Differentiate from other components
It is not used for storing data or handling requests, but specifically for output formatting.
Final Answer:
To define the exact data structure sent back to the client -> Option B
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
Step 1: Identify valid Java class for DTO
Spring Boot supports Java records as concise DTOs with immutable fields and automatic getters.
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.
Final Answer:
public record UserResponse(String name) {} -> Option A
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
Step 1: Understand record fields and JSON mapping
The record fields are name and price, which map directly to JSON keys.
Step 2: Check returned JSON structure
The returned JSON includes both fields with correct types: string for name and number for price.
Final Answer:
{"name":"Book","price":12.5} -> Option C
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
Step 1: Check constructor implementation
The constructor has a parameter but does not assign it to the field, so username remains null.
Step 2: Verify getter correctness
The getter returns the field value, but since field is never set, it returns null.
Final Answer:
Constructor does not assign the username field -> Option D
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
Step 1: Understand security and data exposure
Exposing only needed fields via a Response DTO prevents accidental leaks of sensitive data like passwords.
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.
Final Answer:
Create a separate Response DTO class with only id and email fields -> Option A
Quick Check:
Separate DTOs protect sensitive data [OK]
Hint: Use dedicated DTOs to expose only safe fields [OK]