WebClient is Spring's non-blocking, reactive HTTP client. It lets a Spring application call other HTTP services without tying up a thread while it waits for the response, and it returns results as Reactor Mono and Flux types that you can compose, time out, retry and combine.
It was introduced in Spring 5 as part of Spring WebFlux and replaces RestTemplate for reactive code. It also works fine in Spring MVC applications, and it supports synchronous use through block(). In current Spring versions it sits next to RestClient, the synchronous fluent client added in Spring Framework 6.1.
An HTTP call spends almost all of its time waiting on the network. A blocking client such as RestTemplate parks one thread for that whole wait, so serving many slow downstream calls needs many threads. WebClient runs on an event loop (Reactor Netty by default), so a small number of threads can have thousands of requests in flight.
The practical reasons to choose it are:
Flux.ExchangeFilterFunction) for logging, authentication and metrics.Build one instance per downstream service with WebClient.Builder, then reuse it. In Spring Boot you inject the auto-configured WebClient.Builder, which already carries Boot's codecs and observability settings.
Set connect and response timeouts on the underlying HttpClient. Without them a stalled server can hold a request open far longer than you expect.
The retrieve() method is the simple path: it extracts the body and turns 4xx and 5xx statuses into a WebClientResponseException.
Nothing is sent until something subscribes to the returned Mono or Flux. In a WebFlux controller the framework subscribes for you when you return the publisher.
Use exchangeToMono() instead of retrieve() only when you need the status, headers and body together and want full control. You must consume or release the body in every branch.
Because errors are signals in the reactive stream, you handle them with operators rather than try and catch.
Retry only idempotent calls such as GET. Retrying a POST can create duplicates unless the remote API supports idempotency keys.
Composition is where WebClient earns its place. The two calls below run concurrently, and the total latency is roughly the slower of the two.
| Aspect | RestTemplate | RestClient | WebClient |
|---|---|---|---|
| Model | Blocking | Blocking | Non-blocking, also usable blocking |
| API style | Template methods | Fluent | Fluent |
| Return types | Plain objects | Plain objects | Mono and Flux |
| Streaming | Limited | Limited | Native |
| Status | Maintenance mode | Recommended for sync code | Recommended for reactive code |
If your application is a classic Spring MVC service with no reactive code, RestClient is usually the simpler choice. Pick WebClient when you are already reactive, need streaming, or want to fan out many calls concurrently.
Calling block() on a WebClient result is acceptable in a plain servlet application, a scheduled job or a test. Never call it on an event-loop thread inside a reactive pipeline, because Reactor detects this and throws an IllegalStateException.
For tests, MockWebServer from OkHttp lets you serve canned responses and assert on the request, while WebTestClient is the matching client for testing your own WebFlux endpoints.
No. WebClient is the recommended HTTP client for reactive applications. It is RestTemplate that is in maintenance mode, with RestClient and WebClient as the modern options.
Yes. Add the spring-boot-starter-webflux dependency alongside the MVC starter, and use WebClient for outbound calls. Your server stays servlet-based; only the client is reactive.
The Mono or Flux was never subscribed. Return it from a WebFlux handler, call subscribe(), or use block() where blocking is allowed.
Set a default header on the builder, or register an ExchangeFilterFunction that adds the token per request. A filter is better when the token changes over time.
WebClient is Spring's fluent, non-blocking HTTP client built on Reactor. It is the right tool when you need high concurrency, streaming or parallel composition of remote calls. Configure timeouts on the underlying HttpClient, handle errors with operators such as onStatus, timeout and retryWhen, and never block inside a reactive pipeline. For synchronous code, prefer RestClient.