Bird
0
0

Which of the following method signatures correctly uses @RequestBody to accept a JSON object representing a User in a Spring Boot controller?

easy📝 Syntax Q12 of 15
Spring Boot - REST Controllers
Which of the following method signatures correctly uses @RequestBody to accept a JSON object representing a User in a Spring Boot controller?
Apublic ResponseEntity<String> addUser(@RequestBody User user)
Bpublic ResponseEntity<String> addUser(@RequestParam User user)
Cpublic ResponseEntity<String> addUser(@PathVariable User user)
Dpublic ResponseEntity<String> addUser(User user)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct annotation for JSON body

    @RequestBody is required to tell Spring Boot to convert JSON in the request body to a User object.
  2. Step 2: Check other annotations

    @RequestParam reads query parameters, @PathVariable reads URL path segments, and no annotation means no automatic JSON conversion.
  3. Final Answer:

    public ResponseEntity addUser(@RequestBody User user) -> Option A
  4. Quick Check:

    JSON input needs @RequestBody [OK]
Quick Trick: Use @RequestBody for JSON input, not @RequestParam or @PathVariable [OK]
Common Mistakes:
  • Using @RequestParam instead of @RequestBody
  • Forgetting @RequestBody annotation
  • Confusing @PathVariable with JSON input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes