0
0
Spring Bootframework~10 mins

Entity to DTO mapping in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a DTO class with private fields and public getters.

Spring Boot
public class UserDTO {
    private String [1];

    public String getUsername() {
        return username;
    }
}
Drag options to blanks, or click blank then click option'
Ausername
Buser_id
Cname
DuserName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that does not match the getter method.
Using underscores in variable names in Java.
2fill in blank
medium

Complete the code to map an entity's field to a DTO field in the constructor.

Spring Boot
public UserDTO(UserEntity entity) {
    this.username = entity.[1]();
}
Drag options to blanks, or click blank then click option'
AgetUsername
Bget_name
CgetuserName
DgetUserName
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing in method names.
Using underscores in method names.
3fill in blank
hard

Fix the error in the mapping method that converts an entity to a DTO.

Spring Boot
public static UserDTO fromEntity(UserEntity entity) {
    return new UserDTO([1]);
}
Drag options to blanks, or click blank then click option'
Aentity.convertToDTO()
Bentity.getUsername()
Centity.toDTO()
Dentity
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a field value instead of the whole entity to the constructor.
Calling non-existent methods on the entity.
4fill in blank
hard

Complete the code to complete the method that converts a list of entities to a list of DTOs using streams.

Spring Boot
public static List<UserDTO> fromEntityList(List<UserEntity> entities) {
    return entities.stream()
        .map(entity -> new UserDTO(entity))
        .collect(Collectors.[1]());
}
Drag options to blanks, or click blank then click option'
BtoList
CtoSet
DtoCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method on the entity inside the constructor call.
Using Collectors.toSet() when a list is expected.
5fill in blank
hard

Fill all three blanks to complete the code for a manual mapping method from DTO to entity.

Spring Boot
public UserEntity toEntity() {
    UserEntity entity = new UserEntity();
    entity.set[1](this.[2]);
    entity.setEmail(this.[3]);
    return entity;
}
Drag options to blanks, or click blank then click option'
AUsername
Busername
Cemail
DEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing in setter method names.
Using wrong field names from the DTO.