0
0
Spring Bootframework~10 mins

Why DTOs matter in Spring Boot - Test Your Understanding

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

Complete the code to declare a simple DTO class with a private field and a getter.

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

    public String getName() {
        return name;
    }
}
Drag options to blanks, or click blank then click option'
Aname
Bage
Cemail
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that does not match the getter method.
Forgetting to make the field private.
2fill in blank
medium

Complete the code to create a constructor for the DTO that sets the name field.

Spring Boot
public class UserDTO {
    private String name;

    public UserDTO([1]) {
        this.name = name;
    }
}
Drag options to blanks, or click blank then click option'
AString name
Bint age
Cboolean active
Ddouble salary
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter type that does not match the field.
Using a different parameter name than the field.
3fill in blank
hard

Fix the error in the DTO setter method to correctly set the field.

Spring Boot
public void setName(String name) {
    [1] = name;
}
Drag options to blanks, or click blank then click option'
AsetName
Bname
CUserDTO.name
Dthis.name
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the parameter to itself instead of the field.
Using the class name instead of this.
4fill in blank
hard

Fill both blanks to create a DTO from an entity object.

Spring Boot
public UserDTO([1] user) {
    this.name = user.[2]();
}
Drag options to blanks, or click blank then click option'
AUser
BgetName
CsetName
DUserDTO
Attempts:
3 left
💡 Hint
Common Mistakes
Using the DTO class as parameter type instead of the entity.
Calling a setter method instead of a getter on the entity.
5fill in blank
hard

Fill all three blanks to convert a list of entities to a list of DTOs using streams.

Spring Boot
List<UserDTO> dtos = users.stream()
    .map(user -> new UserDTO(user.[1]()))
    .[2](Collectors.[3]());
Drag options to blanks, or click blank then click option'
AgetName
Bcollect
CtoList
Dstream
Attempts:
3 left
💡 Hint
Common Mistakes
Using a setter method instead of a getter in the map.
Forgetting to collect the stream into a list.
Using incorrect collector methods.