Bird
0
0

Consider this controller method:

medium📝 Debug Q14 of 15
Spring Boot - REST Controllers
Consider this controller method:
@PostMapping("/user")
public String createUser(@RequestBody User user) {
  return user.getEmail();
}

If the client sends an empty request body, what will happen?
ASpring Boot throws an HTTP 400 Bad Request error
BThe method returns null
CThe method returns an empty string
DCompilation error due to missing @RequestBody
Step-by-Step Solution
Solution:
  1. Step 1: Check behavior with empty JSON body

    When @RequestBody expects JSON but receives empty body, Spring Boot cannot create the User object.
  2. Step 2: Understand Spring Boot error handling

    Spring Boot responds with HTTP 400 Bad Request for missing or malformed JSON input.
  3. Final Answer:

    Spring Boot throws an HTTP 400 Bad Request error -> Option A
  4. Quick Check:

    Empty JSON body causes HTTP 400 error [OK]
Quick Trick: Empty JSON body with @RequestBody causes HTTP 400 error [OK]
Common Mistakes:
  • Assuming method returns null or empty string
  • Thinking compilation error occurs
  • Ignoring HTTP error on missing JSON

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes