An explicit RestClient request factory makes the local network transport a deliberate part of a call contract.
Spring RestClient transport: run the factory you configured
The checked local call
RestClientBoundaryTest builds a client with SimpleClientHttpRequestFactory, a 300ms connect timeout and a one-second read timeout. A loopback server responds to GET /risk/receipt-2048 with approved. The test asserts the returned body, so it checks a real HTTP exchange through the selected factory rather than a stubbed RestClient response.
The same factory is reused in the stalled-response and uncertain-write checks. The architecture lesson explains why pinning the transport matters: classpath changes can alter an implicit selection. This test checks only the simple JDK-backed factory. It does not compare Apache, Jetty or JDK HttpClient implementations.
Scope the evidence
The server binds 127.0.0.1 on a random port and is stopped after each test. The response is tiny and local. It does not exercise TLS, DNS, connection pooling, proxies, response-body streaming or a remote upstream. The fixture shows how to wire a factory and inspect a real response; it does not establish production throughput.
Do not share a mutable setup object across concurrent requests without testing its thread behavior. Build the client once with the chosen factory and measure the packaged artifact under load. The deadline fixture supplies one more bounded transport check.
Checked source
var factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(Duration.ofMillis(300));
factory.setReadTimeout(readTimeout);
return RestClient.builder()
.requestFactory(factory)
.baseUrl("http://127.0.0.1:" + upstream.getAddress().getPort())
.build();Verification boundary
RestClientBoundaryTest.explicitFactoryReadsTheUpstreamResponse.
Costs and limits
One loopback exchange with a simple request factory is checked. No TLS, proxy, pool, remote latency or concurrent load is tested.
Common Mistakes
- Do not infer the request factory from the RestClient call site alone.
- Do not call a loopback success test a production latency measurement.
- Do not confuse a real local socket with a real external service.
Read next
Spring RestClient request factories: pin the transport you test, Spring RestClient read timeout: reject a server that accepts but stalls, Spring RestClient 503 response: count attempts before adding a retry, Spring HTTP client timeouts: bound the call inside the request deadline.
