Bird
0
0

Which of the following method signatures correctly defines a POST endpoint in Spring Boot that accepts a JSON payload mapped to a Java object User?

easy📝 Syntax Q3 of 15
Spring Boot - REST Controllers
Which of the following method signatures correctly defines a POST endpoint in Spring Boot that accepts a JSON payload mapped to a Java object User?
A@PostMapping("/createUser") public ResponseEntity<String> createUser(@RequestBody User user) { ... }
B@PostMapping("/createUser") public ResponseEntity<String> createUser(User user) { ... }
C@GetMapping("/createUser") public ResponseEntity<String> createUser(@RequestBody User user) { ... }
D@PostMapping("/createUser") public ResponseEntity<String> createUser(@RequestParam User user) { ... }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct HTTP method annotation

    Use @PostMapping for POST endpoints.
  2. Step 2: Correctly bind JSON payload

    Use @RequestBody to map JSON request body to Java object.
  3. Step 3: Analyze options

    @PostMapping("/createUser") public ResponseEntity createUser(@RequestBody User user) { ... } uses @PostMapping and @RequestBody correctly. @PostMapping("/createUser") public ResponseEntity createUser(User user) { ... } misses @RequestBody, so JSON won't bind properly. @GetMapping("/createUser") public ResponseEntity createUser(@RequestBody User user) { ... } uses @GetMapping, which is incorrect. @PostMapping("/createUser") public ResponseEntity createUser(@RequestParam User user) { ... } uses @RequestParam, which binds query parameters, not JSON body.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    POST + @RequestBody for JSON binding [OK]
Quick Trick: Use @PostMapping with @RequestBody for JSON input [OK]
Common Mistakes:
  • Omitting @RequestBody for JSON payload
  • Using @GetMapping instead of @PostMapping
  • Using @RequestParam for JSON body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes