Bird
Raised Fist0
Spring Bootframework~20 mins

Custom response headers in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Header Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?

Consider this Spring Boot controller method that adds a custom header to the response:

  @GetMapping("/greet")
  public ResponseEntity greet() {
      return ResponseEntity.ok()
          .header("X-Custom-Header", "HelloWorld")
          .body("Greetings!");
  }

What will the HTTP response include?

Spring Boot
  @GetMapping("/greet")
  public ResponseEntity<String> greet() {
      return ResponseEntity.ok()
          .header("X-Custom-Header", "HelloWorld")
          .body("Greetings!");
  }
AHTTP 200 with body 'Greetings!' but no custom headers
BHTTP 200 with body 'Greetings!' and header 'X-Custom-Header: HelloWorld'
CHTTP 404 error because header method is invalid
DHTTP 500 error due to missing @ResponseBody annotation
Attempts:
2 left
💡 Hint

Think about how ResponseEntity builds the response including headers.

📝 Syntax
intermediate
2:00remaining
Which option correctly adds multiple custom headers in Spring Boot?

You want to add two custom headers X-First and X-Second with values One and Two respectively. Which code snippet does this correctly?

Areturn ResponseEntity.ok().header("X-First", "One", "X-Second", "Two").body("Done");
Breturn ResponseEntity.ok().headers(Map.of("X-First", "One", "X-Second", "Two")).body("Done");
Creturn ResponseEntity.ok().header("X-First", "One").header("X-Second", "Two").body("Done");
Dreturn ResponseEntity.ok().header(Map.of("X-First", "One", "X-Second", "Two")).body("Done");
Attempts:
2 left
💡 Hint

Check the method signatures for adding headers in ResponseEntity.

🔧 Debug
advanced
2:00remaining
Why does this code fail to add a custom header?

Examine this Spring Boot controller method:

  @GetMapping("/test")
  public String test(HttpServletResponse response) {
      response.setHeader("X-Test", "Value");
      return "Hello";
  }

But the response does not include the X-Test header. Why?

Spring Boot
  @GetMapping("/test")
  public String test(HttpServletResponse response) {
      response.setHeader("X-Test", "Value");
      return "Hello";
  }
ABecause the header name 'X-Test' is reserved and ignored
BBecause HttpServletResponse cannot set headers in Spring Boot
CBecause setHeader method requires flushing the response manually
DBecause returning String causes Spring to treat it as a view name, not response body
Attempts:
2 left
💡 Hint

Think about what returning a String means in Spring MVC.

🧠 Conceptual
advanced
2:00remaining
What is the best way to add a custom header to all responses in Spring Boot?

You want to add a header X-App-Version with value 1.0 to every HTTP response from your Spring Boot app. Which approach is correct?

ACreate a Filter that adds the header to HttpServletResponse before chain.doFilter()
BAdd the header in every controller method manually
CUse @ControllerAdvice with @ModelAttribute to add the header
DSet the header in application.properties with server.custom.header
Attempts:
2 left
💡 Hint

Think about a centralized way to modify all responses.

❓ state_output
expert
3:00remaining
What headers are present after this Spring Boot controller runs?

Given this controller method:

  @GetMapping("/multi")
  public ResponseEntity multiHeaders() {
      HttpHeaders headers = new HttpHeaders();
      headers.add("X-First", "One");
      headers.add("X-Second", "Two");
      headers.set("X-First", "Uno");
      return new ResponseEntity<>("Done", headers, HttpStatus.OK);
  }

Which headers will the HTTP response include?

Spring Boot
  @GetMapping("/multi")
  public ResponseEntity<String> multiHeaders() {
      HttpHeaders headers = new HttpHeaders();
      headers.add("X-First", "One");
      headers.add("X-Second", "Two");
      headers.set("X-First", "Uno");
      return new ResponseEntity<>("Done", headers, HttpStatus.OK);
  }
AX-First: Uno; X-Second: Two
BX-First: One; X-Second: Two
CX-First: One, Uno; X-Second: Two
DNo headers included
Attempts:
2 left
💡 Hint

Check the difference between add() and set() methods on HttpHeaders.

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

  1. Step 1: Understand what response headers do

    Response headers carry metadata or extra info about the response from server to client.
  2. Step 2: Identify the purpose of custom headers

    Custom headers add extra info beyond standard headers, helping clients handle responses better.
  3. Final Answer:

    To send extra information from the server to the client along with the response -> Option A
  4. 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

  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]
Hint: Start with ok(), then add header(), then body() [OK]
Common Mistakes:
  • Calling header() after body()
  • Using non-existent setHeader() method
  • Wrong method chaining order
3. Given this Spring Boot controller method:
public ResponseEntity<String> getData() {
    return ResponseEntity.ok()
        .header("X-Rate-Limit", "1000")
        .header("X-Request-ID", "abc123")
        .body("Success");
}

What headers will the client receive in the response?
medium
A. No custom headers, only body 'Success'
B. Only X-Rate-Limit: 1000
C. Only X-Request-ID: abc123
D. X-Rate-Limit: 1000 and X-Request-ID: abc123

Solution

  1. Step 1: Analyze header() calls in method

    Two header() calls add "X-Rate-Limit" with "1000" and "X-Request-ID" with "abc123".
  2. Step 2: Confirm both headers are included in response

    ResponseEntity accumulates headers, so both custom headers are sent with the response.
  3. Final Answer:

    X-Rate-Limit: 1000 and X-Request-ID: abc123 -> Option D
  4. Quick Check:

    Multiple header() calls add multiple headers [OK]
Hint: Multiple header() calls add all headers to response [OK]
Common Mistakes:
  • Assuming only last header() call applies
  • Thinking headers overwrite each other
  • Ignoring headers and focusing on body only
4. Identify the error in this Spring Boot code snippet that tries to add a custom header:
@GetMapping("/test")
public ResponseEntity<String> test() {
    ResponseEntity<String> response = ResponseEntity.ok();
    response.header("X-Test", "value");
    return response.body("Done");
}
medium
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

  1. Step 1: Understand ResponseEntity immutability

    ResponseEntity.ok() returns a builder, but here it's assigned to ResponseEntity, which is immutable.
  2. Step 2: header() returns a new builder; original is unchanged

    Calling header() on ResponseEntity does nothing; must chain calls on builder before building.
  3. Final Answer:

    ResponseEntity.ok() returns an immutable object; header() does not modify it in place -> Option B
  4. 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

  1. Step 1: Understand conditional header addition

    ResponseEntity builder allows conditional chaining of header() before body() to add headers only when needed.
  2. 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.
  3. Final Answer:

    Use ResponseEntity builder and conditionally call header() before body() based on login status -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard