getForEntity() sends an HTTP GET request and returns a ResponseEntity containing the converted response body plus response metadata. Use it when the payload alone is not enough, such as when you need response headers. section 7.3.1
Inspecting a response header
A client can fetch an Ingredient while reading the response date, then return the converted body: ResponseEntity<Ingredient> responseEntity = rest.getForEntity("http://localhost:8080/ingredients/{id}", Ingredient.class, ingredientId); log.info("Fetched time: {}", responseEntity.getHeaders().getDate()); return responseEntity.getBody(); section 7.3.1
Choose the call form that matches how the URL is represented:
- Pass URL variables as a variable list, map them by placeholder name, or construct a URI before calling getForEntity(). section 7.3.1
- Create a RestTemplate directly where needed or declare one as a bean for injection. section 7.3
Table 7.2 RestTemplate defines 12 unique operations, each of which is overloaded, providing a total of 41 methods. (view table figure)
| Method |
Description |
| delete(...) |
Performs an HTTP DELETE request on a resource at a specified URL |
| exchange(...) |
Executes a specified HTTP method against a URL, returning a ResponseEntity containing an object mapped from the response body |
| execute(...) |
Executes a specified HTTP method against a URL, returning an object mapped from the response body |
| getForEntity(...) |
Sends an HTTP GET request, returning a ResponseEntity containing an object mapped from the response body |
| getForObject(...) |
Sends an HTTP GET request, returning an object mapped from a response body |
| headForHeaders(...) |
Sends an HTTP HEAD request, returning the HTTP headers for the specified resource URL |
| optionsForAllow(...) |
Sends an HTTP OPTIONS request, returning the Allow header for the specified URL |
| patchForObject(...) |
Sends an HTTP PATCH request, returning the resulting object mapped from the response body |
| postForEntity(...) |
POSTs data to a URL, returning a ResponseEntity containing an object mapped from the response body |
| postForLocation(...) |
POSTs data to a URL, returning the URL of the newly created resource |
| postForObject(...) |
POSTs data to a URL, returning an object mapped from the response body |
| put(...) |
PUTs resource data to the specified URL |
Compare getForEntity() with the other RestTemplate operations, including getForObject(), exchange(), and execute(). section 7.3