Bird
0
0

Identify the error in this Spring Boot POST method: @PostMapping("/addUser") public String addUser(String user) { return "User " + user + " added"; }

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
Identify the error in this Spring Boot POST method: @PostMapping("/addUser") public String addUser(String user) { return "User " + user + " added"; }
AMethod return type should be void
BIncorrect HTTP method annotation, should be @GetMapping
CMissing @PathVariable annotation
DMissing @RequestBody annotation for 'user' parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter binding

    The method expects to receive data in the POST request body, so the parameter must be annotated with @RequestBody to bind JSON or raw data.
  2. Step 2: Verify annotations

    Without @RequestBody, Spring won't map the request body to the 'user' parameter, causing it to be null or empty.
  3. Final Answer:

    Missing @RequestBody annotation for 'user' parameter -> Option D
  4. Quick Check:

    POST body needs @RequestBody on parameter [OK]
Quick Trick: Add @RequestBody to method parameter for POST data [OK]
Common Mistakes:
  • Forgetting @RequestBody on POST method parameters
  • Using @GetMapping for POST endpoints
  • Confusing @PathVariable with request body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes