A. header() method does not exist on ResponseEntity
B. ResponseEntity.ok() returns an immutable object; header() does not modify it in place
C. body() cannot be called after header()
D. Missing @ResponseBody annotation
Solution
Step 1: Understand ResponseEntity immutability
ResponseEntity.ok() returns a builder, but here it's assigned to ResponseEntity, which is immutable.
Step 2: header() returns a new builder; original is unchanged
Calling header() on ResponseEntity does nothing; must chain calls on builder before building.
Final Answer:
ResponseEntity.ok() returns an immutable object; header() does not modify it in place -> Option B
Quick Check:
ResponseEntity is immutable; use builder chaining [OK]
Hint: Chain header() on builder, not on ResponseEntity instance [OK]
Common Mistakes:
Trying to modify ResponseEntity after creation
Confusing builder with built object
Missing chaining of header() before body()
5. You want to add a custom header X-User-Status with value active only if a user is logged in, otherwise no header. Which is the best way to implement this in a Spring Boot controller method?
hard
A. Use ResponseEntity builder and conditionally call header() before body() based on login status
B. Always add header() with value "active" regardless of login status
C. Add header() after calling body() method
D. Modify HttpServletResponse directly without ResponseEntity
Solution
Step 1: Understand conditional header addition
ResponseEntity builder allows conditional chaining of header() before body() to add headers only when needed.
Step 2: Avoid adding headers after body() or always adding unconditionally
Adding header after body() is invalid; always adding ignores condition; modifying HttpServletResponse is less clean.
Final Answer:
Use ResponseEntity builder and conditionally call header() before body() based on login status -> Option A
Quick Check:
Conditionally chain header() before body() [OK]
Hint: Add headers conditionally before body() in ResponseEntity builder [OK]
Common Mistakes:
Adding headers after body() call
Ignoring login condition and always adding header
Directly modifying HttpServletResponse instead of ResponseEntity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.