0
0
Spring Bootframework~10 mins

DTO pattern for data transfer 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 declare a simple DTO class with a private field and a getter.

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

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

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

Spring Boot
public UserDTO([1] username) {
    this.username = username;
}
Drag options to blanks, or click blank then click option'
AUser
BString
Cint
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong data type like int or boolean for a username.
Using a complex type like User instead of String.
3fill in blank
hard

Fix the error in the method that converts an entity to a DTO by filling the blank.

Spring Boot
public static UserDTO fromEntity(User user) {
    return new UserDTO([1]);
}
Drag options to blanks, or click blank then click option'
Auser.getUsername()
Buser.getPassword()
Cuser.getEmail()
Duser.getId()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the user ID or password instead of username.
Passing a field not present in the DTO constructor.
4fill in blank
hard

Fill both blanks to complete the DTO class with a setter and override toString method.

Spring Boot
public class UserDTO {
    private String username;

    public void [1](String username) {
        this.username = username;
    }

    @Override
    public String [2]() {
        return "UserDTO{username='" + username + "'}";
    }
}
Drag options to blanks, or click blank then click option'
AsetUsername
BgetUsername
CtoString
DtoStr
Attempts:
3 left
💡 Hint
Common Mistakes
Using getUsername as a setter method name.
Using a wrong method name like toStr instead of toString.
5fill in blank
hard

Fill both blanks to create a method that converts a list of User entities to a list of UserDTOs.

Spring Boot
public static List<UserDTO> fromEntityList(List<User> users) {
    return users.stream()
        .map(user -> new UserDTO([1]))
        .collect(Collectors.[2]());
}
Drag options to blanks, or click blank then click option'
Auser.getUsername()
BtoList
CtoSet
Duser.getId()
Attempts:
3 left
💡 Hint
Common Mistakes
Using user.getId() instead of user.getUsername() in the map.
Using Collectors.toSet() which changes the collection type.