0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.