Sub topic 6
Sending request bodies with POST, PUT and DELETE
This chapter assumes you can build a RestClient with a base URL (covered earlier), use URI templates, and read responses with retrieve().body(). You also need to know the HTTP methods and status codes, and to be comfortable with Java records.
How a request body is sent
RestClient writes a request body with the body(...) method. You pass a Java object. RestClient picks an HttpMessageConverter that matches the object type and the Content-Type header, and serializes the object. With Jackson on the classpath, which Spring Boot's web starter provides, a record becomes JSON.
Set the content type explicitly with contentType(...). If you leave it out, the client infers it from the converter that handles the object, but stating it makes the request easier to read and avoids surprises.
The call chain always follows the same order: choose the method, set the URI, set headers and body, call retrieve(), then choose how to read the result.
POST: creating a resource
The example below sends a NewTodo as JSON. It returns a ResponseEntity<Void>, so the caller can read the status code and headers such as Location.
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
record NewTodo(String title) {}
ResponseEntity<Void> create(NewTodo newTodo) {
return restClient.post()
.uri("/todos")
.contentType(MediaType.APPLICATION_JSON)
.body(newTodo)
.retrieve()
.toBodilessEntity();
}toBodilessEntity() discards the response body. Use it when the server answers 201 Created or 204 No Content and you do not need the content. If the server returns the created resource and you want it, end the chain with .body(Todo.class) instead.
The request above serializes to {"title":"Write docs"}.
PUT: replacing a resource
PUT has the same shape as POST. The URI identifies the resource, and the body carries its full new state.
record Todo(long id, String title, boolean completed) {}
void update(Todo todo) {
restClient.put()
.uri("/todos/{id}", todo.id())
.contentType(MediaType.APPLICATION_JSON)
.body(todo)
.retrieve()
.toBodilessEntity();
}The {id} placeholder is expanded and URL-encoded by the client. Do not build the path by string concatenation.
DELETE: removing a resource
DELETE normally has no request body, so the chain is shorter.
void delete(long id) {
restClient.delete()
.uri("/todos/{id}", id)
.retrieve()
.toBodilessEntity();
}Why retrieve() matters
Nothing is sent until you call a terminal method such as body(...) or toBodilessEntity(). Writing restClient.post().uri(...).body(x).retrieve() and stopping there sends nothing and returns a ResponseSpec. This is a common mistake when you do not need a result.
retrieve() also turns 4xx and 5xx responses into a RestClientResponseException. A failed POST therefore does not return quietly. The next chapter covers how to handle those errors.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Server answers `415 Unsupported Media Type` | Missing or wrong `Content-Type` | Call `contentType(MediaType.APPLICATION_JSON)` |
| Request body arrives empty or as `{}` | Class has no accessible properties | Use a record, or add getters |
| No request is sent | No terminal call after `retrieve()` | End with `body(...)` or `toBodilessEntity()` |
| Path contains a broken character | Value concatenated into the URI string | Use a `{placeholder}` and pass the value as an argument |
| `NoSuchMethodError` or converter failure on body | No JSON library on the classpath | Add `spring-boot-starter-web` or Jackson |
Exercise
You call restClient.put().uri("/todos/{id}", 7).contentType(MediaType.APPLICATION_JSON).body(todo).retrieve() and stop there. The server logs show no request. Why, and what is the smallest change that fixes it?
Show answer
retrieve() only returns a ResponseSpec. The request is sent when a terminal method runs. Add .toBodilessEntity() (or .body(SomeType.class) if you need the response content).
Exercise
A POST with a NewTodo record returns 415 Unsupported Media Type. Which call do you check first, and which value should it set?
Show answer
Check contentType(...) on the request. Set it to MediaType.APPLICATION_JSON so the body is sent as JSON and the server accepts the media type.