Sub topic 5
RestClient and Spring Boot's RestClient.Builder
This chapter assumes you know Java 17 records, Spring Boot beans and @Configuration, and basic HTTP (methods, status codes, headers, JSON). It covers the RestClient that sits underneath an HTTP interface client. The next chapter feeds it into HttpServiceProxyFactory.
Why the HTTP client matters here
An @HttpExchange interface only describes requests. It does not send them. Something has to execute each call, and in the servlet-based stack that something is RestClient. The proxy translates a method call into a request description and hands it to the RestClient. The RestClient handles the URL, headers, message conversion, and error behavior.
So the settings you put on the RestClient (base URL, default headers, converters, status handlers, timeouts) apply to every method of the interface.
RestClient in a few lines
RestClient is a synchronous client with a fluent API. It was added in Spring Framework 6.1 as the modern replacement for RestTemplate.
import org.springframework.web.client.RestClient;
public class RestClientDemo {
record Todo(Long id, Long userId, String title, boolean completed) {}
public static void main(String[] args) {
RestClient client = RestClient.create("https://jsonplaceholder.typicode.com");
Todo todo = client.get()
.uri("/todos/{id}", 1)
.retrieve()
.body(Todo.class);
System.out.println(todo.title());
}
}The chain reads in order: pick a method (get()), set the URI, call retrieve() to send the request, then body(...) to convert the response. A declarative client generates this kind of chain for you.
The auto-configured RestClient.Builder
RestClient.create(...) gives you a plain client. In a Spring Boot application you should usually start from the builder that Boot provides instead.
Boot registers a RestClient.Builder bean when spring-boot-starter-web (or another starter that brings in Spring MVC) is on the classpath. Boot has already applied its defaults to this builder:
- Message converters, including Jackson configured with your
spring.jackson.*properties. - An HTTP request factory chosen from the libraries on the classpath (for example Apache HttpClient or the JDK client).
- Any
RestClientCustomizerbeans you define.
You inject the builder and call build() to get a client:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
@Configuration
class ApiClientConfig {
@Bean
RestClient todoRestClient(RestClient.Builder builder) {
return builder
.baseUrl("https://jsonplaceholder.typicode.com")
.defaultHeader("Accept", "application/json")
.build();
}
}The builder bean has prototype scope, so each injection point gets a fresh copy. Changing the base URL in one configuration class does not affect another.
Customizing every client in one place
To apply a setting to all clients built from the Boot builder, register a RestClientCustomizer:
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class CommonClientSettings {
@Bean
RestClientCustomizer userAgentCustomizer() {
return builder -> builder.defaultHeader("User-Agent", "todo-app/1.0");
}
}Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No qualifying bean of type RestClient.Builder` | The web starter is missing, or the application is not on the servlet stack | Add `spring-boot-starter-web` or `spring-boot-starter-restclient` (Boot 4), or use `RestClient.builder()` |
| Jackson settings from `application.properties` are ignored | The client was made with `RestClient.create()` or `RestClient.builder()` | Inject `RestClient.Builder` |
| Calls go to the wrong host | The base URL was set on a shared builder | Build from the injected builder in each configuration class, since each injection is a copy |
| `IllegalArgumentException` about an unresolved URI | A relative path was used without a base URL | Call `baseUrl(...)` or pass an absolute URI |
Exercise
Why should you inject RestClient.Builder instead of calling RestClient.create() in a Spring Boot application, and why does each injection point see an independent builder?
Show answer
The injected builder already carries Boot's defaults: Jackson message converters configured from spring.jackson.* properties, the detected HTTP request factory, and any RestClientCustomizer beans. RestClient.create() skips all of these. The builder bean is prototype-scoped, so each injection returns a new copy, and a base URL or header set in one configuration class does not leak into another.