You want to create a POST endpoint that accepts JSON data and returns a confirmation message. Which method signature is correct?
hard📝 Application Q8 of 15
Spring Boot - REST Controllers
You want to create a POST endpoint that accepts JSON data and returns a confirmation message. Which method signature is correct?
A@PostMapping("/create") public void create(@RequestParam User user) { }
B@PostMapping("/create") public String create(User user) { return "User created"; }
C@PostMapping("/create") public String create(@RequestBody User user) { return "User created"; }
D@PostMapping("/create") public String create(@PathVariable User user) { return "User created"; }
Step-by-Step Solution
Solution:
Step 1: Identify how to accept JSON data in POST
Use @RequestBody to bind JSON request body to a Java object.
Step 2: Check method signature correctness
@PostMapping("/create") public String create(@RequestBody User user) { return "User created"; } correctly uses @RequestBody and returns a confirmation string.
Final Answer:
@PostMapping("/create") public String create(@RequestBody User user) { return "User created"; } -> Option C
Quick Check:
Use @RequestBody for JSON input in POST [OK]
Quick Trick:Use @RequestBody to receive JSON in POST methods [OK]
Common Mistakes:
Omitting @RequestBody for JSON
Using @RequestParam for JSON
Using @PathVariable incorrectly
Master "REST Controllers" in Spring Boot
9 interactive learning modes - each teaches the same concept differently