Bird
0
0

Examine this Spring Boot controller method:

medium📝 Debug Q6 of 15
Spring Boot - Request and Response Handling
Examine this Spring Boot controller method:
public ResponseEntity getStatus() {
  return ResponseEntity.ok()
    .body("Success")
    .header("X-Status", "OK");
}

What is the issue with this code?
AThe header() method cannot be called after body(); the order is incorrect
BThe body() method should return void, not ResponseEntity
CResponseEntity.ok() does not support adding headers
DThe method should return a String, not ResponseEntity
Step-by-Step Solution
Solution:
  1. Step 1: Understand ResponseEntity builder chaining

    Methods like header() must be called before body() because body() finalizes the response.
  2. Step 2: Identify incorrect method order

    Calling body() first returns a ResponseEntity, so header() cannot be chained after.
  3. Final Answer:

    The header() method cannot be called after body(); the order is incorrect -> Option A
  4. Quick Check:

    Call header() before body() [OK]
Quick Trick: Call header() before body() in ResponseEntity chain [OK]
Common Mistakes:
  • Calling header() after body()
  • Assuming body() returns builder
  • Confusing ResponseEntity methods order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes