1
1
[[webflux-client]]
2
2
= WebClient
3
3
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.
9
9
10
10
Internally `WebClient` delegates to an HTTP client library. By default, it uses
11
11
https://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
22
22
* `WebClient.create()`
23
23
* `WebClient.create(String baseUrl)`
24
24
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.
28
27
29
- You can use the `WebClient.Builder` for access to further options:
28
+ You can also use `WebClient.builder()` with further options:
30
29
31
30
* `uriBuilderFactory`: Customized `UriBuilderFactory` to use as a base URL.
32
31
* `defaultHeader`: Headers for every request.
33
- * `defaultCookie) `: Cookies for every request.
32
+ * `defaultCookie`: Cookies for every request.
34
33
* `defaultRequest`: `Consumer` to customize every request.
35
34
* `filter`: Client filter for every request.
36
35
* `exchangeStrategies`: HTTP message reader/writer customizations.
@@ -78,19 +77,24 @@ modified copy without affecting the original instance, as the following example
78
77
[[webflux-client-builder-reactor]]
79
78
=== Reactor Netty
80
79
81
- You can customize Reactor Netty settings:
80
+ To customize Reactor Netty settings, simple provide a pre-configured `HttpClient` :
82
81
83
82
====
84
83
[source,java,intent=0]
85
84
[subs="verbatim,quotes"]
86
85
----
87
86
HttpClient httpClient = HttpClient.create().secure(sslSpec -> ...);
88
- ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
89
87
90
- WebClient webClient = WebClient.builder().clientConnector(connector).build();
88
+ WebClient webClient = WebClient.builder()
89
+ .clientConnector(new ReactorClientHttpConnector(httpClient))
90
+ .build();
91
91
----
92
92
====
93
93
94
+
95
+ [[webflux-client-builder-reactor-resources]]
96
+ ==== Resources
97
+
94
98
By default, `HttpClient` participates in the global Reactor Netty resources held in
95
99
`reactor.netty.http.HttpResources`, including event loop threads and a connection pool.
96
100
This 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:
148
152
====
149
153
150
154
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
+
151
190
152
191
[[webflux-client-builder-jetty]]
153
192
=== Jetty
@@ -204,7 +243,7 @@ Spring-managed bean of type `JettyResourceFactory`, as the following example sho
204
243
205
244
206
245
[[webflux-client-retrieve]]
207
- == Using the `retrieve` Method
246
+ == Using `retrieve()`
208
247
209
248
The `retrieve()` method is the easiest way to get a response body and decode it.
210
249
The following example shows how to do so:
@@ -257,7 +296,7 @@ as the following example shows:
257
296
258
297
259
298
[[webflux-client-exchange]]
260
- == Using the `exchange` Method
299
+ == Using `exchange()`
261
300
262
301
The `exchange()` method provides more control than the `retrieve` method. The following example is equivalent
263
302
to `retrieve()` but also provides access to the `ClientResponse`:
0 commit comments