0
0
Spring Bootframework~10 mins

Why service layer matters 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 define a service class in Spring Boot.

Spring Boot
@[1]
public class UserService {
    // service methods
}
Drag options to blanks, or click blank then click option'
AComponent
BController
CRepository
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @Service
Forgetting the @Service annotation
2fill in blank
medium

Complete the code to inject the repository into the service class.

Spring Boot
@Service
public class UserService {
    private final UserRepository userRepository;

    public UserService([1] userRepository) {
        this.userRepository = userRepository;
    }
}
Drag options to blanks, or click blank then click option'
AUserService
BUserRepository
CUserController
DUserEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service class as parameter
Using the entity class instead of repository
3fill in blank
hard

Fix the error in the service method that calls the repository.

Spring Boot
public List<User> getAllUsers() {
    return userRepository.[1]();
}
Drag options to blanks, or click blank then click option'
AgetUsers
BfindAllUsers
CfindAll
DlistAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent repository methods
Misspelling method names
4fill in blank
hard

Fill both blanks to create a service method that saves a user entity.

Spring Boot
public User [1](User user) {
    return userRepository.[2](user);
}
Drag options to blanks, or click blank then click option'
AsaveUser
Bsave
CaddUser
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using repository method names as service method names
Using incorrect repository methods
5fill in blank
hard

Fill all three blanks to create a service method that deletes a user by id.

Spring Boot
public void [1](Long [2]) {
    userRepository.[3]([2]);
}
Drag options to blanks, or click blank then click option'
AdeleteUserById
Bid
CdeleteById
DremoveUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names
Using incorrect repository delete methods