Complete the code to create a DTO class with private fields and public getters.
public class UserDTO { private String [1]; public String getUsername() { return username; } }
The field name must match the getter method name. Here, the getter is getUsername(), so the field should be username.
Complete the code to map an entity's field to a DTO field in the constructor.
public UserDTO(UserEntity entity) {
this.username = entity.[1]();
}The entity's getter method should follow Java naming conventions. Usually, it is getUsername() for a field named username.
Fix the error in the mapping method that converts an entity to a DTO.
public static UserDTO fromEntity(UserEntity entity) {
return new UserDTO([1]);
}The constructor expects a UserEntity object, so pass entity directly to fix the error of passing a method result like a String from getUsername().
Complete the code to complete the method that converts a list of entities to a list of DTOs using streams.
public static List<UserDTO> fromEntityList(List<UserEntity> entities) {
return entities.stream()
.map(entity -> new UserDTO(entity))
.collect(Collectors.[1]());
}The constructor expects the whole entity, so no method call is needed inside the parentheses (blank 1 is empty). The stream collects to a list, so Collectors.toList() is correct.
Fill all three blanks to complete the code for a manual mapping method from DTO to entity.
public UserEntity toEntity() {
UserEntity entity = new UserEntity();
entity.set[1](this.[2]);
entity.setEmail(this.[3]);
return entity;
}The setter method uses PascalCase for the field name: setUsername. The DTO field is username (camelCase). The email field is accessed as email in the DTO and set via setEmail in the entity.