Complete the code to create a basic Spring Boot 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 annotation to mark the main class as a Spring Boot application.
import org.springframework.boot.autoconfigure.SpringBootApplication; @[1] public class MyApp {}
The @SpringBootApplication annotation marks the main class as a Spring Boot app.
Fix the error in the code to correctly inject a service in Spring Boot.
import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.[1]; @Service public class MyService { // service methods }
The @Autowired annotation is used to inject dependencies in Spring Boot.
Fill both blanks to create a REST controller with a GET endpoint in Spring Boot.
import org.springframework.web.bind.annotation.[1]; import org.springframework.web.bind.annotation.[2]; @[1]("/hello") public class HelloController { @[2]("/") public String greet() { return "Hello, Spring Boot!"; } }
@RestController marks the class as a REST controller, and @GetMapping maps HTTP GET requests.
Fill all three blanks to define a Spring Boot application with a service and controller.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; @SpringBootApplication public class [1] { public static void main(String[] args) { SpringApplication.[2]([3].class, args); } } @Service class GreetingService { public String greet() { return "Hello from service!"; } } @RestController class GreetingController { private final GreetingService service; @Autowired public GreetingController(GreetingService service) { this.service = service; } @GetMapping("/greet") public String greet() { return service.greet(); } }
The main class is named Application, and SpringApplication.run() starts it.