Bird
0
0

Which of the following is the correct way to add a custom header named X-Custom-Header with value 12345 using ResponseEntity in Spring Boot?

easy📝 Syntax Q12 of 15
Spring Boot - Request and Response Handling
Which of the following is the correct way to add a custom header named X-Custom-Header with value 12345 using ResponseEntity in Spring Boot?
Areturn ResponseEntity.header("X-Custom-Header", "12345").body(data).ok();
Breturn ResponseEntity.body(data).header("X-Custom-Header", "12345");
Creturn ResponseEntity.ok().header("X-Custom-Header", "12345").body(data);
Dreturn ResponseEntity.setHeader("X-Custom-Header", "12345").body(data);
Step-by-Step Solution
Solution:
  1. Step 1: Recall ResponseEntity builder pattern

    ResponseEntity.ok() starts building a 200 OK response, then header() adds headers, then body() sets the body.
  2. Step 2: Check method chaining order

    Correct chaining is ResponseEntity.ok().header(...).body(...). Other orders or methods like setHeader() do not exist or cause errors.
  3. Final Answer:

    return ResponseEntity.ok().header("X-Custom-Header", "12345").body(data); -> Option C
  4. Quick Check:

    Use ok() then header() then body() [OK]
Quick Trick: Start with ok(), then add header(), then body() [OK]
Common Mistakes:
  • Calling header() after body()
  • Using non-existent setHeader() method
  • Wrong method chaining order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes