liveBook · Manning

HTTP PUT method

As explained in Spring in Action, Sixth Edition

An HTTP PUT request sends a complete representation to a resource's URL, conventionally replacing the resource's existing representation.

HTTP PUT sends a representation from the client to a resource URL, conventionally replacing the resource there. A replacement should include every property: omitted values may become null, and omitted nested data such as order tacos may be removed. section 7.1.3

Replacing an order

A Spring MVC handler can accept an order identifier in the URL and a complete TacoOrder in the request body. Setting the path identifier on the submitted object and saving it replaces the stored order. Use PATCH instead when changing only selected fields, such as an address. section 7.1.3

@PutMapping(path="/{orderId}", consumes="application/json")
public TacoOrder putOrder(
                      @PathVariable("orderId") Long orderId,
                      @RequestBody TacoOrder order) {
  order.setId(orderId);
  return repo.save(order);
}

The controller listing shows @PutMapping accepting JSON, binding the path and request body, assigning the resource identifier, and saving the complete representation. section 7.1.3

A client can also send a replacement with RestTemplate.put(). The method accepts a URI or string URL, serializes the supplied object, substitutes URL variables, and returns void. section 7.3.2

public void updateIngredient(Ingredient ingredient) {
  rest.put("http://localhost:8080/ingredients/{id}",
        ingredient, ingredient.getId());
}

This client example replaces an ingredient by sending an Ingredient object and using its identifier as the URL variable. section 7.3.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains replacement semantics, omitted properties, Spring mappings, request binding, persistence, and client serialization without covering framework internals.
Examples
Uses order replacement, order-address changes, TacoOrder controller code, and ingredient replacement through RestTemplate.
What it assumes
Assumes resources, URLs, request bodies, and REST endpoints while demonstrating the relevant Spring annotations and client method.
What you can do after
Apply it via Spring MVC or RestTemplate to send complete resource representations to URL endpoints.
How it connects
Contrasts HTTP PATCH with PUT and connects both methods to REST, Spring MVC, request bodies, and resource persistence.

Also explained in

Compare how 8 books explain HTTP PUT method →

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.