Complete the code to define a Spring Boot main application class.
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 declare a Spring component for dependency injection.
import org.springframework.stereotype.[1]; @[1] public class Service { // service logic }
The @Component annotation marks a class as a Spring-managed component for dependency injection.
Fix the error in the code to inject a repository into a service using constructor injection.
import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService([1] userRepository) { this.userRepository = userRepository; } }
The constructor parameter must be the type of the repository to inject, here UserRepository.
Fill both blanks to create a REST controller method that handles GET requests and returns a list.
import org.springframework.web.bind.annotation.[1]; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class ItemController { @[2]("/items") public List<String> getItems() { return List.of("apple", "banana", "cherry"); } }
The method should be annotated with @GetMapping to handle GET requests. The import must match the annotation used.
Fill all three blanks to define a Spring Data JPA repository interface for an entity User with Long id.
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.[1]; @[1] public interface UserRepository extends JpaRepository<[2], [3]> { }
The interface should be annotated with @Repository. It extends JpaRepository with the entity User and ID type Long.