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
Recall & Review
beginner
What is a Response DTO in Spring Boot?
A Response DTO (Data Transfer Object) is a simple Java class used to define the structure of data sent from the server to the client. It helps control what data is exposed in the API response.
Click to reveal answer
beginner
Why use a Response DTO instead of returning entity objects directly?
Using Response DTOs helps keep internal data safe, allows customizing the response format, and reduces data sent over the network by including only needed fields.
Click to reveal answer
intermediate
Which annotation is commonly used to create immutable Response DTOs in Spring Boot?
The @Value annotation from Lombok is often used to create immutable Response DTOs with final fields and no setters.
Click to reveal answer
intermediate
How do you map entity data to a Response DTO?
You can map entity data to a Response DTO manually in the service layer or use libraries like MapStruct to automate the mapping.
Click to reveal answer
beginner
What is the typical structure of a Response DTO class?
A Response DTO usually contains private fields for the data, public getters, and optionally constructors or builder methods. It does not contain business logic.
Click to reveal answer
What is the main purpose of a Response DTO in Spring Boot?
ATo define the data sent from server to client
BTo store data in the database
CTo handle HTTP requests
DTo configure security settings
✗ Incorrect
Response DTOs define the structure of data sent back to clients in API responses.
Which of these is a benefit of using Response DTOs?
AExposing all database fields directly
BReducing data sent over the network
CAdding business logic to responses
DReplacing service layer logic
✗ Incorrect
Response DTOs help reduce data sent by including only necessary fields.
Which annotation helps create immutable Response DTOs with Lombok?
A@Entity
B@Controller
C@Value
D@Service
✗ Incorrect
@Value creates immutable classes with final fields and no setters.
Where is the best place to map entity data to a Response DTO?
AIn the service layer
BIn the controller
CIn the repository
DIn the database
✗ Incorrect
Mapping is typically done in the service layer to keep controllers simple.
What should a Response DTO NOT contain?
APrivate fields
BPublic getters
CConstructors
DBusiness logic
✗ Incorrect
Response DTOs should not contain business logic; they only hold data.
Explain what a Response DTO is and why it is useful in Spring Boot applications.
Think about how you send data back to a client safely and clearly.
You got /3 concepts.
Describe how you would create and use a Response DTO to send user data without exposing sensitive fields.
Focus on selecting only safe fields for output.
You got /3 concepts.
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]