A custom response header is an additional piece of information added to the HTTP response by the server. In Spring Boot, you can add these headers to provide extra data to the client beyond the standard headers.
Click to reveal answer
beginner
How do you add a custom response header using Spring Boot's ResponseEntity?
You can add custom headers by creating an HttpHeaders object, adding your headers to it, and then passing it to ResponseEntity. For example: HttpHeaders headers = new HttpHeaders(); headers.add("X-Custom-Header", "value"); return new ResponseEntity<>(body, headers, HttpStatus.OK);
Click to reveal answer
intermediate
What is the purpose of the @ResponseHeader annotation in Spring Boot?
Spring Boot does not have a @ResponseHeader annotation. Instead, you add custom headers using HttpServletResponse, ResponseEntity, or WebFilter. This is important to know to avoid confusion.
Click to reveal answer
beginner
How can you add a custom header using HttpServletResponse in a Spring Boot controller?
You can inject HttpServletResponse into your controller method and call response.addHeader("X-Custom-Header", "value") before returning the response body.
Click to reveal answer
beginner
Why might you want to add custom response headers in a web application?
Custom headers can provide extra information like security tokens, version info, or debugging data. They help clients understand or handle the response better without changing the response body.
Click to reveal answer
Which class in Spring Boot is commonly used to add custom response headers?
AHttpServletRequest
BHttpHeaders
CRequestBody
DPathVariable
✗ Incorrect
HttpHeaders is used to set custom headers in the response.
How do you add a custom header using HttpServletResponse?
Aresponse.addHeader("X-Name", "value")
Bresponse.setBody("X-Name", "value")
Cresponse.getHeader("X-Name")
Dresponse.removeHeader("X-Name")
✗ Incorrect
The addHeader method adds a new header to the response.
What is the correct way to return a response with custom headers using ResponseEntity?
Anew ResponseEntity<>(headers, body, status)
Bnew ResponseEntity<>(status, headers, body)
Cnew ResponseEntity<>(body, headers, status)
Dnew ResponseEntity<>(body, status, headers)
✗ Incorrect
The constructor takes body, headers, then status in that order.
Which of these is NOT a reason to add custom response headers?
ATo change the HTTP method
BTo send extra info like tokens
CTo provide version info
DTo help with debugging
✗ Incorrect
Custom headers do not change the HTTP method.
Can you add multiple custom headers in Spring Boot response?
AOnly if you use @ResponseHeader
BNo, only one header is allowed
COnly with XML configuration
DYes, by adding multiple headers to HttpHeaders
✗ Incorrect
You can add as many headers as needed using HttpHeaders.
Explain how to add a custom response header in a Spring Boot controller using ResponseEntity.
Think about how you build the response with headers included.
You got /3 concepts.
Describe why custom response headers are useful in web applications.
Consider what extra data headers can carry.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of adding custom response headers in a Spring Boot application?
easy
A. To send extra information from the server to the client along with the response
B. To change the HTTP method of the request
C. To modify the request body before processing
D. To encrypt the response data automatically
Solution
Step 1: Understand what response headers do
Response headers carry metadata or extra info about the response from server to client.
Step 2: Identify the purpose of custom headers
Custom headers add extra info beyond standard headers, helping clients handle responses better.
Final Answer:
To send extra information from the server to the client along with the response -> Option A
Quick Check:
Custom headers = extra info sent [OK]
Hint: Custom headers add info to server replies, not change requests [OK]
Common Mistakes:
Confusing headers with request body changes
Thinking headers change HTTP methods
Assuming headers encrypt data automatically
2. 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
A. return ResponseEntity.header("X-Custom-Header", "12345").body(data).ok();
B. return ResponseEntity.body(data).header("X-Custom-Header", "12345");
C. return ResponseEntity.ok().header("X-Custom-Header", "12345").body(data);
D. return ResponseEntity.setHeader("X-Custom-Header", "12345").body(data);
Solution
Step 1: Recall ResponseEntity builder pattern
ResponseEntity.ok() starts building a 200 OK response, then header() adds headers, then body() sets the body.
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.
Final Answer:
return ResponseEntity.ok().header("X-Custom-Header", "12345").body(data); -> Option C
Quick Check:
Use ok() then header() then body() [OK]
Hint: Start with ok(), then add header(), then body() [OK]
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.