WebClient is Spring's non-blocking, reactive HTTP client, built on Project Reactor and part of the spring-webflux module. It replaces the blocking RestTemplate for calls that must not tie up a thread while waiting on the network, and it can also be used from ordinary Spring MVC applications. Requests are described with a fluent API and the response is returned as a Mono or Flux that you compose with other reactive operators.
In a typical Spring Boot 3 application you inject a WebClient.Builder, set a base URL and defaults once, and reuse the built client for every call. Since Spring Framework 6.1 there is also RestClient for the synchronous case, so WebClient is now chosen mainly for its reactive and streaming abilities.
RestTemplate blocks the calling thread until the response arrives. That is fine for a handful of requests, but a service that fans out to many upstream APIs wastes threads waiting. WebClient frees the thread while the request is in flight and continues when data arrives.
| Client | Model | Typical use |
|---|---|---|
RestTemplate | Blocking, in maintenance mode | Legacy code |
RestClient (Spring 6.1+) | Blocking, fluent API | New synchronous code |
WebClient | Non-blocking, reactive | WebFlux apps, streaming, parallel fan-out |
If you are on virtual threads (Java 21+), blocking clients scale much better than before, so WebClient is worth choosing mainly when you already use reactive types or need streaming.
Build the client once from the auto-configured builder so that Spring Boot's codecs and observability settings apply.
WebClient.create() also works for quick experiments, but the builder gives you a single place for base URL, default headers, filters and codec limits. A built WebClient is immutable and thread-safe.
The retrieve() method is the simple path: it sends the request and lets you pick how to decode the body.
Nothing is sent until something subscribes to the returned Mono or Flux. In a WebFlux controller you simply return it and the framework subscribes. All HTTP methods are available through get(), post(), put(), patch(), delete() and method(...). Use bodyValue for a plain object and body(Publisher, Class) when the request body is itself a reactive stream.
By default retrieve() turns 4xx and 5xx responses into a WebClientResponseException. You can map specific statuses to your own exceptions with onStatus, and add a retry with backoff for transient failures.
Only retry idempotent requests, and filter on the failures that are safe to repeat. For full control over the response, including status and headers, use exchangeToMono, but remember you must consume or release the body yourself in that case.
WebClient is an abstraction over an HTTP connector. Spring Boot uses Reactor Netty by default, and timeouts are set on the connector, not on WebClient itself.
You can also apply .timeout(Duration) on the Mono to cap the whole operation. Large responses may exceed the default in-memory buffer of 256 KB; raise it with codecs(c -> c.defaultCodecs().maxInMemorySize(...)) or stream the body instead.
An ExchangeFilterFunction wraps every request and is the right place for logging, adding auth headers or propagating trace IDs. For OAuth2, Spring Security provides ServerOAuth2AuthorizedClientExchangeFilterFunction, which attaches access tokens automatically.
For tests, point the client at MockWebServer from OkHttp, or use WebTestClient when testing your own WebFlux endpoints. Avoid calling .block() inside reactive code running on an event-loop thread; Reactor will throw an error. Blocking is acceptable at the edges, such as in a main method or a test.
No. You can add spring-boot-starter-webflux to a Spring MVC application and use WebClient there. Both stacks can coexist, and your controllers stay servlet-based.
It is in maintenance mode: it still works, but the Spring team recommends RestClient for new blocking code and WebClient for reactive code.
The pipeline is lazy. Without a subscriber, either through subscribe(), block() or returning it from a reactive controller, the request is never sent.
Call .block() on the Mono. This is fine outside the event loop, but if you only need blocking calls, RestClient is the simpler choice.
WebClient is Spring's reactive HTTP client: build it once from WebClient.Builder, describe requests fluently, decode with bodyToMono or bodyToFlux, and handle failures with onStatus and retryWhen. Configure timeouts on the Reactor Netty connector, keep the pipeline lazy until subscribed, and prefer RestClient when you only need simple blocking calls.