Bird
0
0

What is wrong with this code snippet for adding a custom header?

medium📝 Debug Q7 of 15
Spring Boot - Request and Response Handling
What is wrong with this code snippet for adding a custom header?
HttpHeaders headers = new HttpHeaders();
headers.set("X-User", null);
return ResponseEntity.ok().headers(headers).body("User info");
AResponseEntity.ok() cannot accept headers
BSetting header value to null causes the header to be omitted
CHttpHeaders does not have set() method
DHeader names cannot contain dashes
Step-by-Step Solution
Solution:
  1. Step 1: Check HttpHeaders set() method behavior

    Setting a header value to null removes or omits that header from the response.
  2. Step 2: Confirm effect on response

    The header "X-User" will not appear in the HTTP response because its value is null.
  3. Final Answer:

    Setting header value to null causes the header to be omitted -> Option B
  4. Quick Check:

    Null header values omit headers [OK]
Quick Trick: Null header values remove headers from response [OK]
Common Mistakes:
  • Assuming null values become empty strings
  • Thinking headers with dashes are invalid
  • Believing ResponseEntity.ok() cannot accept headers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes