Complete the code to define a REST controller class in Spring Boot.
import org.springframework.web.bind.annotation.[1]; @[1] public class HelloController { // controller methods }
The @RestController annotation marks the class as a REST controller in Spring Boot.
Complete the code to create a GET endpoint that returns a greeting string.
@RestController public class GreetingController { @[1]("/greet") public String greet() { return "Hello!"; } }
The @GetMapping annotation maps HTTP GET requests to the method.
Fix the error in the code by completing the annotation to handle POST requests.
@RestController public class DataController { @[1]("/data") public String saveData() { return "Data saved"; } }
The @PostMapping annotation is used to handle HTTP POST requests.
Fill both blanks to create a REST controller with a GET endpoint returning JSON.
import org.springframework.web.bind.annotation.[1]; import org.springframework.web.bind.annotation.[2]; @[1] public class UserController { @[2]("/user") public User getUser() { return new User("Alice", 30); } }
@RestController marks the class as a REST controller, and @GetMapping maps GET requests to the method.
Fill both blanks to create a REST controller with a POST endpoint that accepts JSON and returns a confirmation string.
import org.springframework.web.bind.annotation.[1]; import org.springframework.web.bind.annotation.[2]; import org.springframework.web.bind.annotation.RequestBody; @[1] public class OrderController { @[2]("/order") public String placeOrder(@RequestBody Order order) { return "Order placed for " + order.getItem(); } }
The class is marked with @RestController. The method uses @PostMapping to handle POST requests. The @RequestBody annotation binds the JSON request to the method parameter.