Challenge - 5 Problems
DTO vs Entity Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why separate DTOs from entities in Spring Boot?
In a Spring Boot application, why is it beneficial to separate Data Transfer Objects (DTOs) from entity classes?
Attempts:
2 left
💡 Hint
Think about how exposing entities directly might affect your API and database design.
✗ Incorrect
Separating DTOs from entities helps protect the internal database structure from external clients. It also allows the API to evolve independently without changing the database schema.
❓ component_behavior
intermediate2:00remaining
What happens if you expose entities directly in API responses?
Consider a Spring Boot REST API that returns entity objects directly as JSON responses. What is a likely consequence of this approach?
Attempts:
2 left
💡 Hint
Think about what data entities contain and how that might affect clients.
✗ Incorrect
Entities often contain fields that should not be exposed externally, such as internal IDs or audit information. Returning entities directly can leak this data.
❓ state_output
advanced2:30remaining
What is the output when mapping entity to DTO with missing fields?
Given an entity with fields
id, name, and password, and a DTO with only id and name, what will be the output JSON when mapping the entity to the DTO and returning it in a Spring Boot REST controller?Spring Boot
public record UserEntity(Long id, String name, String password) {}
public record UserDTO(Long id, String name) {}
// Mapping method
public UserDTO toDTO(UserEntity entity) {
return new UserDTO(entity.id(), entity.name());
}
// Controller method returns toDTO(userEntity)Attempts:
2 left
💡 Hint
Only fields declared in the DTO will be serialized in the JSON response.
✗ Incorrect
The DTO only contains id and name, so the JSON output excludes password. This protects sensitive data from being exposed.
📝 Syntax
advanced2:00remaining
Identify the correct DTO mapping syntax in Spring Boot
Which of the following code snippets correctly maps a UserEntity to a UserDTO in Spring Boot using a constructor?
Spring Boot
public record UserEntity(Long id, String name, String password) {}
public record UserDTO(Long id, String name) {}Attempts:
2 left
💡 Hint
Records use accessor methods without 'get' prefix.
✗ Incorrect
Java records provide accessor methods named after the fields, so userEntity.id() and userEntity.name() are correct.
🔧 Debug
expert3:00remaining
Why does exposing entities cause a security risk in Spring Boot?
A developer returns JPA entity objects directly from a REST controller. Later, they notice sensitive fields like passwords are exposed in API responses. Why does this happen?
Attempts:
2 left
💡 Hint
Think about what happens when an object is converted to JSON without filtering.
✗ Incorrect
Entities contain all fields, including sensitive ones. Without DTOs or annotations to exclude fields, all data is serialized and sent to clients, causing security risks.