Sub topic 8
Choosing a request factory and setting timeouts
This chapter assumes you can build a RestClient from RestClient.Builder (see the chapter on creating and configuring a client) and that you know the difference between a connect timeout and a read timeout.
What a request factory does
RestClient does not open sockets itself. It hands each request to a ClientHttpRequestFactory, which creates the request object backed by a real HTTP library. The factory decides which library is used and which timeouts apply.
If you set no timeouts, a slow or unresponsive server can hold the calling thread for a very long time. In a synchronous client, that thread is usually a servlet request thread, so a few stuck calls can exhaust your pool. Set timeouts on every client that talks to a network service.
Available factories
Spring ships several implementations. Spring Boot picks one for you based on what is on the classpath, so check which one you actually get before tuning it.
| Factory | Underlying library | Notes |
|---|---|---|
| `JdkClientHttpRequestFactory` | `java.net.http.HttpClient` | No extra dependency. Supports HTTP/2. |
| `HttpComponentsClientHttpRequestFactory` | Apache HttpClient 5 | Connection pooling controls and many options. |
| `JettyClientHttpRequestFactory` | Jetty HttpClient | Useful if you already use Jetty. |
| `ReactorClientHttpRequestFactory` | Reactor Netty | Useful if Reactor Netty is already present. |
| `SimpleClientHttpRequestFactory` | `HttpURLConnection` | Basic and limited. |
For most applications, the JDK factory is a good default because it needs no extra library. Choose Apache HttpClient when you need fine control over the connection pool, proxies, or TLS settings.
Setting a connect timeout and a read timeout
The two timeouts live in different places with the JDK factory. The connect timeout is a property of the JDK HttpClient. The read timeout is set on the factory.
import java.net.http.HttpClient;
import java.time.Duration;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
RestClient timeoutClient(RestClient.Builder builder) {
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(2))
.build();
JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(httpClient);
requestFactory.setReadTimeout(Duration.ofSeconds(5));
return builder
.baseUrl("https://api.example.com")
.requestFactory(requestFactory)
.build();
}The two values mean different things:
- The connect timeout limits how long the client waits to establish the TCP (and TLS) connection.
- The read timeout limits how long the client waits for response data once the request is sent.
When either limit is exceeded, the call fails with a ResourceAccessException (a subclass of RestClientException). This differs from RestClientResponseException, which signals that the server answered with a 4xx or 5xx status. A timeout means no usable response arrived at all.
import org.springframework.web.client.ResourceAccessException;
String fetch(RestClient client) {
try {
return client.get().uri("/slow").retrieve().body(String.class);
} catch (ResourceAccessException e) {
return "timed out or unreachable: " + e.getMessage();
}
}Setting timeouts in Spring Boot configuration
Spring Boot 3.4 and later can apply timeouts without code. The spring.http.client.* properties configure the factory that Boot creates for the auto-configured RestClient.Builder.
spring.http.client.connect-timeout=2s
spring.http.client.read-timeout=5sThis works only when you build your client from the injected RestClient.Builder. If you create a factory by hand and pass it to requestFactory(...), as in the example above, your factory replaces the one Boot configured and the properties no longer apply to that client.
Common mistakes
- Setting only the connect timeout. A server that accepts the connection and then stalls is still able to block your thread.
- Treating the read timeout as a limit on the whole call. It limits waiting for data, so a server that trickles bytes can keep a call alive longer than the value suggests. If you need a hard overall deadline, enforce it at a higher level.
- Setting the timeout on a factory and then calling
requestFactory(...)on a second builder. Each built client keeps the factory it was given. - Choosing very short values without measuring. Base your read timeout on the slowest legitimate response from the API, plus a margin.
Exercise
Your RestClient was built with a hand-made JdkClientHttpRequestFactory, and you also set spring.http.client.read-timeout=1s in application.properties. The calls still wait 5 seconds before failing. Why, and which exception do you catch when they fail?
Show answer
The factory passed to requestFactory(...) replaces the one Spring Boot would configure, so the property is ignored. The 5-second value comes from setReadTimeout on your own factory. Either remove the custom factory and use the injected RestClient.Builder, or change the value on the factory. A timeout surfaces as ResourceAccessException, not RestClientResponseException.