liveBook · Manning

RestTemplate

As explained in Spring in Action, Sixth Edition

RestTemplate is a Spring client API for making synchronous HTTP requests and mapping HTTP responses to Java objects.

RestTemplate is Spring’s straightforward, synchronous REST client. It handles request creation, execution, response interpretation, object mapping, and common HTTP boilerplate when one Spring application consumes another application’s API. section 7.3

Create an instance directly or expose it as a Spring bean so client code can reuse it. section 7.3

Choose an operation according to the HTTP request and the response details you need:

  • Use getForObject() to return a response body mapped to a Java domain object. section 7.3.1
  • Use getForEntity() when the body and response metadata, such as headers, are needed through ResponseEntity. section 7.3.1
  • Use put() to serialize an object into an HTTP PUT request; the method returns void. section 7.3.2
  • Use delete() to remove a resource identified by a URL and optional URL variables. section 7.3.3
  • Use postForObject(), postForLocation(), or postForEntity() to receive the created object, its Location URI, or both. section 7.3.4

Fetching an ingredient

A client can retrieve an ingredient by substituting an ID into the URL and deserializing the response into Ingredient: rest.getForObject("http://localhost:8080/ingredients/{id}", Ingredient.class, ingredientId). URL variables can instead be supplied with a Map or a constructed URI. section 7.3.1

Creating, updating, and deleting ingredients

A client can send an Ingredient with postForObject(), replace one with put(), or remove one with delete(). These methods use the same URL-variable patterns while differing in request and return behavior. section 7.3.2 section 7.3.3 section 7.3.4

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

Browse the operation table to compare RestTemplate’s twelve unique operations and their overloaded method forms. section 7.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains setup, URL variables, object mapping, response metadata, and CRUD-style methods, while deferring reactive clients.
Examples
Demonstrates retrieving, creating, updating, and deleting Taco Cloud Ingredient resources with concrete Java methods.
What it assumes
Assumes basic Java objects and HTTP resource concepts while teaching Spring’s request, mapping, and URL-variable handling from practical examples.
What you can do after
Apply it via Java client methods that issue HTTP requests and map response bodies or metadata into usable results.
How it connects
Connects REST APIs, HTTP methods, JSON deserialization, ResponseEntity, Spring HATEOAS, and WebClient.

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.