11[[webflux-client]]
22= WebClient
33
4- Spring WebFlux includes a reactive, non-blocking `WebClient` for performing HTTP requests
5- using a functional-style API that exposes Reactor `Flux` and `Mono` types , see
6- <<web-reactive.adoc#webflux-reactive-libraries>>. The client relies on the same
7- <<web-reactive.adoc#webflux-codecs,codecs>> that WebFlux server applications use to work
8- with request and response content.
4+ Spring WebFlux includes a reactive, non-blocking `WebClient` for HTTP requests. The client
5+ has a functional, fluent API with reactive types for declarative composition , see
6+ <<web-reactive.adoc#webflux-reactive-libraries>>. WebFlux client and server rely on the
7+ same non-blocking <<web-reactive.adoc#webflux-codecs,codecs>> to encode and decode request
8+ and response content.
99
1010Internally `WebClient` delegates to an HTTP client library. By default, it uses
1111https://github.com/reactor/reactor-netty[Reactor Netty], there is built-in support for
@@ -22,15 +22,14 @@ The simplest way to create a `WebClient` is through one of the static factory me
2222* `WebClient.create()`
2323* `WebClient.create(String baseUrl)`
2424
25- The preceding methods use Reactor Netty `HttpClient` from `io.projectreactor.netty:reactor-netty`
26- with default settings and participates in global resources for event loop threads and
27- a connection pool. See <<webflux-client-builder-reactor, Reactor Netty configuration>>.
25+ The above methods use the Reactor Netty `HttpClient` with default settings and expect
26+ `io.projectreactor.netty:reactor-netty` to be on the classpath.
2827
29- You can use the `WebClient.Builder` for access to further options:
28+ You can also use `WebClient.builder()` with further options:
3029
3130* `uriBuilderFactory`: Customized `UriBuilderFactory` to use as a base URL.
3231* `defaultHeader`: Headers for every request.
33- * `defaultCookie) `: Cookies for every request.
32+ * `defaultCookie`: Cookies for every request.
3433* `defaultRequest`: `Consumer` to customize every request.
3534* `filter`: Client filter for every request.
3635* `exchangeStrategies`: HTTP message reader/writer customizations.
@@ -78,19 +77,24 @@ modified copy without affecting the original instance, as the following example
7877[[webflux-client-builder-reactor]]
7978=== Reactor Netty
8079
81- You can customize Reactor Netty settings:
80+ To customize Reactor Netty settings, simple provide a pre-configured `HttpClient` :
8281
8382====
8483[source,java,intent=0]
8584[subs="verbatim,quotes"]
8685----
8786 HttpClient httpClient = HttpClient.create().secure(sslSpec -> ...);
88- ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
8987
90- WebClient webClient = WebClient.builder().clientConnector(connector).build();
88+ WebClient webClient = WebClient.builder()
89+ .clientConnector(new ReactorClientHttpConnector(httpClient))
90+ .build();
9191----
9292====
9393
94+
95+ [[webflux-client-builder-reactor-resources]]
96+ ==== Resources
97+
9498By default, `HttpClient` participates in the global Reactor Netty resources held in
9599`reactor.netty.http.HttpResources`, including event loop threads and a connection pool.
96100This is the recommended mode, since fixed, shared resources are preferred for event loop
@@ -148,6 +152,41 @@ instances use shared resources, as the following example shows:
148152====
149153
150154
155+ [[webflux-client-builder-reactor-timeout]]
156+ ==== Timeouts
157+
158+ To configure a connection timeout:
159+
160+ ====
161+ [source,java,intent=0]
162+ [subs="verbatim,quotes"]
163+ ----
164+ import io.netty.channel.ChannelOption;
165+
166+ HttpClient httpClient = HttpClient.create()
167+ .tcpConfiguration(client ->
168+ client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000));
169+ ----
170+ ====
171+
172+ To configure a read and/or write timeout values:
173+
174+ ====
175+ [source,java,intent=0]
176+ [subs="verbatim,quotes"]
177+ ----
178+ import io.netty.handler.timeout.ReadTimeoutHandler;
179+ import io.netty.handler.timeout.WriteTimeoutHandler;
180+
181+ HttpClient httpClient = HttpClient.create()
182+ .tcpConfiguration(client ->
183+ client.doOnConnected(conn -> conn
184+ .addHandlerLast(new ReadTimeoutHandler(10))
185+ .addHandlerLast(new WriteTimeoutHandler(10))));
186+ ----
187+ ====
188+
189+
151190
152191[[webflux-client-builder-jetty]]
153192=== Jetty
@@ -204,7 +243,7 @@ Spring-managed bean of type `JettyResourceFactory`, as the following example sho
204243
205244
206245[[webflux-client-retrieve]]
207- == Using the `retrieve` Method
246+ == Using `retrieve()`
208247
209248The `retrieve()` method is the easiest way to get a response body and decode it.
210249The following example shows how to do so:
@@ -257,7 +296,7 @@ as the following example shows:
257296
258297
259298[[webflux-client-exchange]]
260- == Using the `exchange` Method
299+ == Using `exchange()`
261300
262301The `exchange()` method provides more control than the `retrieve` method. The following example is equivalent
263302to `retrieve()` but also provides access to the `ClientResponse`:
0 commit comments