Sub topic 10
Configuring timeouts
This chapter assumes you can already build a TodoClient proxy with HttpServiceProxyFactory and register it as a bean. It also assumes Spring Boot 3.4 or later, or Boot 4, and Java 17 or later.
Why timeouts matter
A declarative client hides the HTTP call behind a method call, so a slow server looks like a slow method. Without limits, a stalled remote service can hold your request threads until the pool is exhausted. Timeouts turn that stall into a fast, catchable failure.
The proxy does not set timeouts itself. The RestClient underneath delegates to a ClientHttpRequestFactory, which wraps a concrete HTTP library (JDK HttpClient, Apache HttpClient, Jetty, or HttpURLConnection). The timeouts belong to that factory.
The two timeouts
| Timeout | Limits | Typical value |
|---|---|---|
| Connect timeout | Time to establish the connection | 1 to 3 seconds |
| Read timeout | Time waiting for response data after the request is sent | Depends on the endpoint, often 5 to 30 seconds |
The read timeout is not a limit on the whole call. It limits waiting for data. A server that sends a byte every few seconds can keep the call alive for much longer.
Option 1: Spring Boot properties
Since Spring Boot 3.4, the auto-configured RestClient.Builder reads shared settings from spring.http.client:
spring.http.client.connect-timeout=2s
spring.http.client.read-timeout=5sAny client built from the injected builder, including the TodoClient configuration from the earlier chapter, picks these up with no code change. The values apply to every client built from that builder, so they suit a sensible default.
Option 2: Per-client settings in code
When one client needs different limits, build the request factory yourself and pass it to the builder:
import java.time.Duration;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@Configuration
class TodoClientConfig {
@Bean
TodoClient todoClient(RestClient.Builder builder) {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(2))
.withReadTimeout(Duration.ofSeconds(5));
ClientHttpRequestFactory requestFactory =
ClientHttpRequestFactoryBuilder.detect().build(settings);
RestClient restClient = builder
.baseUrl("https://jsonplaceholder.typicode.com")
.requestFactory(requestFactory)
.build();
return HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build()
.createClient(TodoClient.class);
}
}ClientHttpRequestFactoryBuilder.detect() picks whichever supported HTTP library is on the classpath. The same settings work for any of them, so you do not need library-specific timeout code.
Option 3: Group properties on Boot 4
On Spring Boot 4 with @ImportHttpServices, the group properties can hold timeouts next to the base URL:
spring.http.serviceclient.todos.base-url=https://jsonplaceholder.typicode.com
spring.http.serviceclient.todos.connect-timeout=2s
spring.http.serviceclient.todos.read-timeout=5sThis gives each group its own limits without writing a configuration class.
Handling a timeout
A timeout is an I/O failure, not an HTTP status. RestClient wraps it in ResourceAccessException, so the HttpClientErrorException handling from the error chapter does not catch it:
import org.springframework.stereotype.Service;
import org.springframework.web.client.ResourceAccessException;
@Service
class TodoLookup {
private final TodoClient todoClient;
TodoLookup(TodoClient todoClient) {
this.todoClient = todoClient;
}
Todo findOrFallback(long id) {
try {
return todoClient.getTodo(id);
} catch (ResourceAccessException e) {
return new Todo(id, null, "unavailable", false);
}
}
}ResourceAccessException also covers refused connections and DNS failures. Check getCause() if you need to tell a timeout from other network errors.
Common mistakes
- Setting only the read timeout. An unreachable host can still block on connect.
- Choosing one read timeout for every endpoint. A report download needs more time than a lookup, so give slow endpoints their own client.
- Retrying on timeout without checking idempotency. Retrying a
POSTmay create the resource twice.
Exercise
A TodoClient call to a slow server fails after 5 seconds. Which exception does your calling code see, and which timeout setting triggered it?
Show answer
It sees ResourceAccessException, with a timeout exception as its cause. The read timeout triggered it, because the connection was established and the client then waited longer than 5 seconds for response data.