Bird
0
0

Examine the following Spring Boot POST method. What is the main issue that would prevent it from correctly receiving the item value from the request body?

medium📝 Debug Q7 of 15
Spring Boot - REST Controllers
Examine the following Spring Boot POST method. What is the main issue that would prevent it from correctly receiving the item value from the request body?
@PostMapping("/addItem")
public String addItem(String item) {
    return "Item added: " + item;
}
AThe method parameter lacks the @RequestBody annotation to bind the request body.
BThe method should return void instead of String.
CThe @PostMapping annotation should specify consumes = "application/xml".
DThe method name must be addItemPost to handle POST requests.
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameter binding

    To bind the HTTP request body to a method parameter, @RequestBody is required.
  2. Step 2: Analyze the method

    The parameter 'String item' lacks @RequestBody, so Spring won't map the POST request body to it.
  3. Step 3: Evaluate other options

    Return type String is valid; consumes attribute is optional; method name does not affect mapping.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Missing @RequestBody causes binding failure [OK]
Quick Trick: Add @RequestBody to bind POST request body [OK]
Common Mistakes:
  • Forgetting @RequestBody on method parameters
  • Assuming method name affects HTTP method mapping
  • Misunderstanding return type requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes