Sub topic 11
Calling a versioned API with RestClient
This chapter assumes you have a versioned endpoint like the /accounts/{id} example from earlier chapters, with the header X-API-Version as the version source. You should know the basics of RestClient (building one and calling retrieve()). Here you will learn how the client side puts the version on each request.
Why the client needs support
A server that reads the version from a header expects every caller to send it. You could add .header("X-API-Version", "2.0") to each call by hand. That spreads the header name across your code, and a typo silently falls back to the server's default version.
Spring Framework 7 moves this into the client. You configure an ApiVersionInserter once. After that, each call states only the version it wants.
Configuring the inserter
An ApiVersionInserter knows where in the request the version goes. ApiVersionInserter.useHeader writes it to a header. The inserter must use the same location as the server's version resolver.
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.client.RestClient;
public class AccountClient {
private final RestClient client = RestClient.builder()
.baseUrl("http://localhost:8080")
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
.build();
public String fetchAccount(long id, String version) {
return client.get()
.uri("/accounts/{id}", id)
.apiVersion(version)
.retrieve()
.body(String.class);
}
}The call fetchAccount(1, "2.0") sends GET /accounts/1 with the header X-API-Version: 2.0. The server then picks the version = "2.0+" handler.
The inserter has factory methods for the other server-side strategies:
| Server strategy | Client inserter |
|---|---|
| Request header | `ApiVersionInserter.useHeader("X-API-Version")` |
| Query parameter | `ApiVersionInserter.useQueryParam("version")` |
| Path segment | `ApiVersionInserter.usePathSegment(0)` |
| Media type parameter | `ApiVersionInserter.useMediaTypeParam("version")` |
For the path segment, the number is the index of the segment that holds the version. Check the exact method names against your Spring version.
Setting a default version for the client
If most calls use one version, set it on the builder with defaultApiVersion("1.1"). Calls that do not call apiVersion(...) then send that version. A per-call apiVersion overrides it. This pins your client to a known version, so a server-side default change cannot alter its behavior unnoticed.
Failure behavior
The client does not check versions. It sends whatever you give it. If the server rejects the version because it is unsupported, or because a required version is missing, it answers with a 4xx status, normally 400. retrieve() turns that into an HttpClientErrorException.
import org.springframework.web.client.HttpClientErrorException;
try {
new AccountClient().fetchAccount(1, "9.9");
} catch (HttpClientErrorException e) {
System.out.println(e.getStatusCode());
}Common mistakes
- The inserter location differs from the server resolver. A header-based server ignores a version sent as a query parameter and serves its default version.
- Version strings are written inconsistently. Use
"2.0"everywhere, not sometimes"2". - The version is added by hand and also through the inserter. Pick one.
Exercise
Your server resolves the version from a query parameter named v. Which inserter should your RestClient use, and what happens if you use useHeader("v") instead?
Show answer
Use ApiVersionInserter.useQueryParam("v"). With useHeader("v") the version goes in a header that the server never reads. The server sees no version and applies its default version, or returns an error if versions are required. The call may succeed but return the wrong version.