Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Compare java.net.http.HttpClient with Apache HttpClient, OkHttp and Spring's RestClient or WebClient for a service that calls many downstream APIs.

The JDK HttpClient supports HTTP/1.1 and HTTP/2, synchronous and asynchronous calls, and needs no dependency, but offers few tuning knobs and no interceptors. Apache HttpClient and OkHttp give finer connection pool control, interceptors and mature retry options. Spring's RestClient (synchronous) and WebClient (reactive) are higher-level abstractions that run on top of one of those engines and add message conversion, error handling and observability. For a service calling many downstream APIs, we choose by observability integration, pool control, proxy and TLS needs and team familiarity, and whatever we choose, we set timeouts and pool sizes explicitly.

Why this question is asked

A service that calls many downstream APIs spends much of its life inside an HTTP client, so the client's behaviour under load, its pool limits, timeouts and retries, shapes the service's reliability. The Java ecosystem has several good options, and they are not all at the same level: some are engines that move bytes, others are abstractions on top of engines. Interviewers ask for this comparison to see whether we understand that layering and choose for concrete reasons. In this answer, we will separate engines from abstractions, look at each client's strengths, compare them in a table, walk through the criteria for choosing, and show the explicit configuration that matters whatever we pick.

1Engines and Abstractions

The first step is to see that these options are not all alternatives to each other. The JDK HttpClient, Apache HttpClient 5 and OkHttp are engines: they open connections, manage the pool, speak HTTP/1.1 or HTTP/2, and handle TLS. Spring's RestClient and WebClient are abstractions: they give a fluent API, convert JSON to objects with the application's message converters, map error statuses to exceptions, and integrate with Spring's observability, but they delegate the actual HTTP work to an engine through a request factory or connector.

So a realistic choice is often a pair: RestClient on top of Apache HttpClient, or WebClient on top of Reactor Netty. Spring Boot picks an engine automatically based on what is on the class path and lets us choose one explicitly through configuration; check your Boot version for the property names.

Step throughAbstractions over engines
  1. Application codecalls typed methods, or @HttpExchange interfaces
  2. RestClient (blocking) or WebClient (reactive)JSON conversion, status handling, observations
  3. Request factory or connectoradapts the abstraction to an engine
  4. EngineJDK HttpClient, Apache HttpClient 5, Jetty, Reactor Netty
  5. Connectionspool, keep-alive, TLS, HTTP/1.1 or HTTP/2

5 layers, from the top down. Step through them one at a time.

2The JDK HttpClient

The JDK client, in java.net.http since Java 11, supports HTTP/1.1 and HTTP/2, synchronous send() and asynchronous sendAsync(), WebSocket, proxies through ProxySelector, and TLS through SSLContext. Its biggest advantage is that it needs no dependency and is maintained with the JDK. Its limits show at scale: there is no API to set a maximum pool size or per-host limit, idle timeouts are set only through JDK system properties, there are no interceptors, and retries are basic. For a few downstream calls it is a solid, simple choice, especially with virtual threads making send() cheap.

3Apache HttpClient 5 and OkHttp

Apache HttpClient 5 is the most configurable engine. Its pooling connection manager sets the maximum total connections and the maximum per route, meaning per target host, and it has explicit timeouts for connecting, for socket reads and for acquiring a connection from the pool, the one people forget. It supports interceptors, configurable retry strategies, proxies, and detailed TLS settings, with both classic blocking and async APIs. Its default pool limits are small, a few connections per route, so they must be raised for busy services; check your version's defaults.

OkHttp, from Square, is popular on Android and in Kotlin, with a clean API, application and network interceptors, transparent HTTP/2, automatic retries on some connection failures, and a connection pool with sensible defaults. In server-side Java it is used mostly where teams already know it or where a library depends on it; Spring does not ship a request factory for it in recent versions, so check your version before planning on RestClient over OkHttp.

Clients for a service with many downstreams
ClientStrengthsWeak spots
JDK HttpClientno dependency, HTTP/2, sync and asynclittle pool control, no interceptors
Apache HttpClient 5per-route pools, pool timeout, retries, TLSmore configuration, older API style
OkHttpclean API, interceptors, HTTP/2less Spring integration on the server
RestClientfluent, JSON, errors, observationsblocking; quality depends on the engine
WebClientreactive, streaming, backpressureReactor learning curve for blocking teams

4Spring's RestClient and WebClient

RestClient, added in Spring Framework 6.1, is the modern synchronous client and the successor to RestTemplate, which is in maintenance mode. WebClient is the reactive client, returning Mono and Flux, best for applications already built on WebFlux or that need streaming and many concurrent calls without many threads. Both can back declarative HTTP interfaces, interfaces annotated with @HttpExchange or @GetExchange that Spring implements for us, which keeps calling code very small.

Java
@Bean
RestClient inventoryClient(RestClient.Builder builder) { // Boot's builder adds observations and converters
var pool = PoolingHttpClientConnectionManagerBuilder.create()
.setMaxConnTotal(200)
.setMaxConnPerRoute(50) // per downstream host
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(2))
.setSocketTimeout(Timeout.ofSeconds(5)).build())
.build();
var http = HttpClients.custom()
.setConnectionManager(pool)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectionRequestTimeout(Timeout.ofMilliseconds(500)).build()) // wait for a pooled connection
.build();
return builder.baseUrl("https://inventory.internal")
.requestFactory(new HttpComponentsClientHttpRequestFactory(http))
.build();
}
Item item = inventoryClient.get().uri("/items/{id}", 42).retrieve().body(Item.class); // Item[id=42, ...]

This is the pairing many teams settle on for a service with many downstreams: RestClient for the API and observability, Apache HttpClient for pool control. Using the injected RestClient.Builder matters, because Spring Boot configures it with the application's JSON converters and with Micrometer observations, so every call produces metrics and trace spans tagged with the client name, method, status and URI template.

5How to Choose, and What to Set Whatever We Choose

For many downstream APIs, four criteria decide. Observability: do we get per-host latency, error metrics and trace propagation without custom code? Spring's clients give this through Micrometer on any engine. Pool control: can we limit connections per downstream so one slow service cannot take all of them, and set a timeout for waiting on the pool? Apache HttpClient is strongest here. Proxy and TLS needs: corporate proxies, mutual TLS or custom trust stores are supported by all engines, but with different APIs. Team familiarity: a team that knows WebClient and Reactor will be productive with it; a blocking team on Java 21 or later is usually better served by RestClient on virtual threads.

Whatever we choose, we never rely on defaults for the settings that cause incidents. We set the connect timeout, the read or response timeout, the pool acquisition timeout, the maximum connections in total and per host, and the idle connection eviction, explicitly and per downstream when their behaviour differs. A client with an infinite read timeout and a small default pool is the most common cause of a service hanging when one dependency slows down.

6Follow-up questions

Is RestTemplate still acceptable?

Existing code using RestTemplate keeps working, but it is in maintenance mode and the Spring team recommends RestClient for new code, and has announced plans to deprecate RestTemplate in a future version; check your version. RestClient can even be created from an existing RestTemplate's configuration, which eases migration.

Should a blocking Spring MVC service use WebClient?

Only if it needs reactive features, such as streaming responses or composing many concurrent calls. Calling block() on WebClient everywhere adds Reactor complexity without benefit; RestClient, especially on virtual threads, is simpler for blocking services.

Why does the pool acquisition timeout matter so much?

When a downstream slows down, its connections stay busy and the pool runs out. Without an acquisition timeout, new requests wait for a connection indefinitely, so threads pile up and the slowness spreads to our callers. A short acquisition timeout, a few hundred milliseconds, turns that into a fast, visible failure.

7Conclusion

In this answer, we separated engines, the JDK HttpClient, Apache HttpClient and OkHttp, from Spring's RestClient and WebClient abstractions that run on top of them. The JDK client offers HTTP/2, sync and async calls with no dependency, while Apache HttpClient and OkHttp give more pool tuning, interceptors and retry options. We choose by observability integration, pool control, proxy and TLS needs and team familiarity, often RestClient over Apache HttpClient for many downstreams, and we always configure timeouts and pool sizes explicitly.

What to say in the interviewAbout 90 seconds aloud

Open with

I would first separate engines from abstractions. The JDK HttpClient, Apache HttpClient 5 and OkHttp are engines that manage connections, pools, HTTP/2 and TLS. Spring's RestClient and WebClient sit on top of an engine and add a fluent API, JSON conversion, error handling and Micrometer observations.

Then, if they want more

The JDK client supports HTTP/1.1 and HTTP/2, sync and async calls and needs no dependency, which is great for a few calls, but it has little pool control, no per-host limits and no interceptors. Apache HttpClient gives the most control: total and per-route pool limits, separate connect, socket and pool acquisition timeouts, interceptors and retry strategies.

OkHttp has a clean API, interceptors and HTTP/2, but less Spring integration on the server side. RestClient is the modern blocking Spring client replacing RestTemplate, and WebClient is the reactive one for WebFlux or streaming.

For a service calling many downstream APIs, my usual choice is RestClient, built from Spring Boot's builder so we get metrics and tracing per call, on top of Apache HttpClient for pool control, running on virtual threads. If the team is already reactive, WebClient on Reactor Netty. The criteria are observability, pool control, proxy and TLS requirements, and team familiarity.

And whichever client, I set connect, read and pool acquisition timeouts and pool sizes explicitly per downstream, because defaults like an infinite read timeout are what turn one slow dependency into an outage.

About this answer

Last updated
12 November 2026
Versions it names
Java 11, Java 21, Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare java.net.http.HttpClient with Apache HttpClient, OkHttp and Spring's RestClient...": https://scrivio-interview-library.netlify.app/java/networking/aebd684ce5/