📖 Scenario: You are building a simple Spring Boot web service that returns a greeting message. You want to add a custom response header to the HTTP response to provide extra information to the client.
🎯 Goal: Create a Spring Boot controller that returns a greeting message and includes a custom response header named X-Custom-Header with the value MyHeaderValue.
📋 What You'll Learn
Create a Spring Boot REST controller class named GreetingController.
Add a GET endpoint at /greet that returns a String greeting message.
Add a custom response header X-Custom-Header with the value MyHeaderValue to the HTTP response.
💡 Why This Matters
🌍 Real World
Custom response headers are often used to send extra information like security tokens, version info, or metadata from a server to a client in web applications.
💼 Career
Knowing how to add custom headers in Spring Boot is important for backend developers to control HTTP responses and improve API communication.
Progress0 / 4 steps
1
Create the GreetingController class
Create a public class named GreetingController annotated with @RestController.
Spring Boot
Hint
Use @RestController annotation above the class declaration.
2
Add a GET endpoint method
Inside GreetingController, add a public method named greet annotated with @GetMapping("/greet") that returns the String "Hello, World!".
Spring Boot
Hint
Use @GetMapping("/greet") to map the method to the GET request path.
3
Add HttpServletResponse parameter
Modify the greet method to accept a parameter of type HttpServletResponse named response.
Spring Boot
Hint
Add HttpServletResponse response as a parameter to the greet method.
4
Set the custom response header
Inside the greet method, use the response object to add a header named X-Custom-Header with the value MyHeaderValue before returning the greeting string.
Spring Boot
Hint
Use response.addHeader("X-Custom-Header", "MyHeaderValue") to add the header.
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.