Bird
0
0

What is wrong with this Java Spring controller method returning 204?

medium📝 Debug Q7 of 15
Rest API - HTTP Status Codes
What is wrong with this Java Spring controller method returning 204?
@DeleteMapping("/item/{id}")
public ResponseEntity deleteItem(@PathVariable String id) {
    // delete logic
    return ResponseEntity.noContent().body("Deleted");
}
APathVariable annotation is missing
BResponseEntity.noContent() cannot have a body
CDeleteMapping should return 200 instead
DMethod should return void
Step-by-Step Solution
Solution:
  1. Step 1: Understand ResponseEntity.noContent()

    It sets status 204 No Content which must not include a body.
  2. Step 2: Identify the error in returning a body with noContent()

    Calling body() adds content, violating 204 rules.
  3. Final Answer:

    ResponseEntity.noContent() cannot have a body -> Option B
  4. Quick Check:

    204 response must not include body [OK]
Quick Trick: noContent() means no response body allowed [OK]
Common Mistakes:
MISTAKES
  • Adding body to 204 response
  • Confusing 204 with 200 OK
  • Incorrect annotation usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes