Sub topic 3
Creating and configuring a RestClient
This chapter assumes you know Java 17 (records, lambdas), basic Spring Boot (beans, @Configuration, dependency injection), HTTP methods, status codes and headers, and JSON. It covers how to get a RestClient instance and how to set defaults that apply to every request it sends.
Three ways to create a client
A RestClient is immutable and thread-safe once built, so you create it once and reuse it. There are three common entry points.
| Entry point | Use it when |
|---|---|
| `RestClient.create()` | You want a quick client with no defaults. |
| `RestClient.create(String baseUrl)` | You need only a base URL. |
| `RestClient.builder()` or an injected `RestClient.Builder` | You need default headers, a request factory, interceptors, or other settings. |
The first two are shortcuts for the builder. This standalone example runs without Spring Boot, because RestClient is part of spring-web:
import org.springframework.web.client.RestClient;
public class QuickStart {
public static void main(String[] args) {
RestClient client = RestClient.create("https://api.example.com");
String body = client.get()
.uri("/todos/1")
.retrieve()
.body(String.class);
System.out.println(body);
}
}Using the Spring Boot builder
In a Spring Boot 3.2+ application, auto-configuration registers a RestClient.Builder bean. Prefer it over RestClient.builder(). The injected builder already has Boot's JSON message converters (backed by your ObjectMapper configuration) and any RestClientCustomizer beans applied.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
@Configuration
class TodoClientConfig {
@Bean
RestClient todoRestClient(RestClient.Builder builder) {
return builder
.baseUrl("https://api.example.com")
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.build();
}
}The two settings here do the following:
baseUrlis prepended to every relative URI you pass touri(...). A request to/todos/1goes tohttps://api.example.com/todos/1.defaultHeaderadds a header to every request. You can still override it on a single request.
The builder is a prototype bean
Boot registers RestClient.Builder with prototype scope. Every injection point receives a fresh builder, so configuring it in one bean does not change the builder in another.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
@Configuration
class TwoClientsConfig {
@Bean
RestClient todoClient(RestClient.Builder builder) {
return builder.baseUrl("https://todos.example.com").build();
}
@Bean
RestClient billingClient(RestClient.Builder builder) {
return builder.baseUrl("https://billing.example.com").build();
}
}Two beans of the same type make injection ambiguous. Inject by parameter name matching the bean name, as the TodoService in a later chapter does with todoRestClient, or add @Qualifier.
Deriving a variant with mutate()
Because a built client cannot be changed, use mutate() to get a builder pre-filled with its settings. Change what you need and build a new client. The original is untouched.
RestClient adminClient = todoRestClient.mutate()
.defaultHeader("X-Role", "admin")
.build();This is useful when one service needs a handful of requests with extra headers and you do not want to repeat the base configuration.
Sharing settings with RestClientCustomizer
When several clients need the same setup, such as a common header or interceptor, register a RestClientCustomizer bean. Boot applies every customizer to the auto-configured builder.
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class SharedClientSettings {
@Bean
RestClientCustomizer userAgentCustomizer() {
return builder -> builder.defaultHeader("User-Agent", "todo-app/1.0");
}
}Every RestClient.Builder injected after this bean exists will already send that User-Agent header. Interceptors are covered in their own chapter.
Common mistakes
- Building a new
RestClientinside every method call. Build once and inject it. - Forgetting that a base URL with a path (
https://api.example.com/v2) is combined with the relative URI, so/todosbecomes/v2/todos. - Expecting a change to one injected builder to affect another bean. Each injection gets its own copy.
Exercise
A teammate injects RestClient.Builder into two beans. In the first bean they call builder.baseUrl("https://a.example.com"). In the second bean they build a client without setting a base URL. Does the second client use https://a.example.com? Why or why not?
Show answer
No. Spring Boot registers RestClient.Builder as a prototype bean, so each injection point receives its own fresh builder. The base URL set on the first bean's builder does not affect the second. To share settings, use a RestClientCustomizer bean, or call mutate() on an already built client.