Complete the code to create a POST endpoint using @PostMapping.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @[1]("/submit") public String submitData() { return "Data submitted!"; } }
The @PostMapping annotation is used to map HTTP POST requests to the method.
Complete the code to accept POST requests at the path '/addUser'.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @[1]("/addUser") public String addUser() { return "User added!"; } }
@PostMapping("/addUser") maps POST requests sent to '/addUser' to the method.
Fix the error in the code to correctly handle POST requests.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProductController { @PostMapping("/addProduct") public String addProduct() { return "Product added!"; } @[1]("/deleteProduct") public String deleteProduct() { return "Product deleted!"; } }
The method deleteProduct should use @DeleteMapping to handle DELETE requests, not POST.
Fill both blanks to create a POST endpoint that accepts JSON data and returns a confirmation.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @[1]("/createOrder") public String createOrder(@[2] String orderData) { return "Order received: " + orderData; } }
@PostMapping maps the POST request, and @RequestBody tells Spring to read the JSON data from the request body.
Fill all three blanks to create a POST endpoint that accepts a JSON object, extracts a field, and returns a message.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class FeedbackController { @[1]("/submitFeedback") public String submitFeedback(@[2] Map<String, String> feedback) { String comment = feedback.get("comment"); return "Feedback received: " + [3]; } }
The @PostMapping annotation maps the POST request. @RequestBody binds the JSON to a Map. The variable comment holds the feedback comment to return.