0
0
Spring Bootframework~20 mins

Why DTOs matter in Spring Boot - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DTO Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use DTOs in Spring Boot applications?

In a Spring Boot app, what is the main reason to use Data Transfer Objects (DTOs)?

ATo separate the internal data model from what is exposed to clients, improving security and flexibility.
BTo replace the database entities entirely and avoid using JPA repositories.
CTo automatically generate database tables without writing SQL scripts.
DTo speed up the application by caching all data in memory.
Attempts:
2 left
💡 Hint

Think about how you protect your private information when sharing only what is needed.

component_behavior
intermediate
2:00remaining
What happens if you return entities directly from a Spring Boot REST controller?

Consider a Spring Boot REST controller that returns JPA entities directly as JSON. What is a likely problem?

AThe application will run faster because no extra mapping is needed.
BThe database schema will automatically update to match the JSON structure.
CThe REST controller will cache the entities and never refresh data.
DThe JSON may expose sensitive fields and cause infinite recursion errors due to bidirectional relationships.
Attempts:
2 left
💡 Hint

Think about what happens when you share your full address book with everyone, including private notes.

state_output
advanced
2:00remaining
What is the output of this Spring Boot controller method using a DTO?

Given the following code snippet, what JSON will the client receive?

Spring Boot
public record UserEntity(Long id, String username, String password) {}

public record UserDTO(Long id, String username) {}

@GetMapping("/user/{id}")
public UserDTO getUser(@PathVariable Long id) {
    UserEntity user = userService.findById(id);
    return new UserDTO(user.id(), user.username());
}
A{"id":1,"username":"alice","password":"secret"}
B{"id":1,"username":"alice"}
C{"username":"alice"}
D{"id":1}
Attempts:
2 left
💡 Hint

Look at which fields the DTO includes compared to the entity.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly maps an entity to a DTO in Spring Boot?

Choose the correct Java code that converts a UserEntity to a UserDTO.

AUserDTO dto = new UserDTO(userEntity.getId());
BUserDTO dto = new UserDTO(userEntity.id, userEntity.username, userEntity.password);
CUserDTO dto = new UserDTO(userEntity.getId(), userEntity.getUsername());
DUserDTO dto = new UserDTO(userEntity.getUsername());
Attempts:
2 left
💡 Hint

Remember the DTO only has id and username fields.

🔧 Debug
expert
3:00remaining
Why does this Spring Boot app throw a StackOverflowError when returning entities directly?

Given two JPA entities User and Address with bidirectional @OneToMany and @ManyToOne relationships, returning User directly from a REST controller causes a StackOverflowError. Why?

ABecause the JSON serializer tries to serialize User, then Address, then User again endlessly due to circular references.
BBecause the database connection is lost during serialization.
CBecause the REST controller method is missing @ResponseBody annotation.
DBecause the User entity has no default constructor.
Attempts:
2 left
💡 Hint

Think about what happens when you look in a mirror facing another mirror.