0
0
Spring Bootframework~10 mins

Service calling repository 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 inject the repository into the service class.

Spring Boot
public class UserService {
    private final UserRepository [1];

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}
Drag options to blanks, or click blank then click option'
AuserRepo
BuserRepository
Crepository
Drepo
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the constructor parameter
Forgetting to declare the field
2fill in blank
medium

Complete the code to annotate the service class for Spring to detect it as a service.

Spring Boot
[1]
public class UserService {
    // service methods
}
Drag options to blanks, or click blank then click option'
A@Repository
B@Component
C@Controller
D@Service
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Repository instead of @Service
Forgetting to annotate the class
3fill in blank
hard

Fix the error in the service method to call the repository's findById method correctly.

Spring Boot
public User getUserById(Long id) {
    return userRepository.[1](id).orElse(null);
}
Drag options to blanks, or click blank then click option'
AgetOne
BgetById
CfindById
DfindOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using deprecated methods like getOne
Using methods that do not return Optional
4fill in blank
hard

Fill both blanks to complete the service method that saves a user using the repository.

Spring Boot
public User saveUser(User [1]) {
    return userRepository.[2]([1]);
}
Drag options to blanks, or click blank then click option'
Auser
Bsave
Cadd
Dentity
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names
Using wrong repository method names like add
5fill in blank
hard

Fill all three blanks to complete the service method that deletes a user by ID using the repository.

Spring Boot
public void deleteUserById([1] [2]) {
    userRepository.[3]([2]);
}
Drag options to blanks, or click blank then click option'
ALong
Bid
CdeleteById
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter types
Using wrong method names like delete