Complete the code to define a Spring Boot application entry point.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.[1](Application.class, args); } }
The SpringApplication.run() method starts the Spring Boot application.
Complete the code to inject a JPA repository into a service class.
import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.[1]; @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } // service methods }
The @Autowired annotation tells Spring to inject the repository automatically.
Fix the error in the repository interface declaration.
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<[1], Long> { }
The repository must extend JpaRepository with the entity class, here UserEntity.
Fill both blanks to create a REST controller method that fetches a user by ID.
import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/[1]") public UserEntity getUserById(@PathVariable [2] id) { return userService.findById(id); } }
The path variable name should match the placeholder in the URL, here 'id'. The parameter type is Long for the user ID.
Fill all three blanks to define a Spring Data JPA query method that finds users by their email.
import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; public interface UserRepository extends JpaRepository<UserEntity, Long> { Optional<UserEntity> findBy[1]([2] [3]); }
The method name must be findByEmail. The parameter type is String and the parameter name is email.