liveBook · Manning

HTTP header

As explained in Spring in Action, Sixth Edition

A response header is an HTTP field carrying metadata from a server response separately from its payload body.

An HTTP response header carries metadata from the server separately from the response payload body. Use headers when the client needs response details beyond the returned resource. section 7.3.1

With Spring's RestTemplate, getForObject() returns the payload directly. Choose getForEntity() when the client also needs the response headers; the returned ResponseEntity wraps the payload and exposes those details. section 7.3.1

Reading the Date header

Fetch an ingredient with getForEntity(), inspect responseEntity.getHeaders().getDate(), and then obtain the resource through responseEntity.getBody().

public Ingredient getIngredientById(String ingredientId) {
  ResponseEntity<Ingredient> responseEntity =
      rest.getForEntity("http://localhost:8080/ingredients/{id}",
          Ingredient.class, ingredientId);
  log.info("Fetched time: {}",
          responseEntity.getHeaders().getDate());
  return responseEntity.getBody();
}

See the complete RestTemplate example that retrieves a resource, reads its response headers, and returns the payload body. section 7.3.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains header access through ResponseEntity and getForEntity(), but does not show server-side header creation.
Examples
Demonstrates inspecting the Date header while fetching an ingredient resource.
What you can do after
Apply it via RestTemplate to inspect response metadata and retrieve the accompanying payload body.
How it connects
Connects HTTP response headers with RestTemplate, ResponseEntity, response payloads, and getForEntity().

Also explained in

Compare how 4 books explain HTTP header →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.