|
1 | 1 | [[webflux-client]]
|
2 | 2 | = WebClient
|
3 | 3 |
|
4 |
| -The `spring-webflux` module includes a non-blocking, reactive client for HTTP requests |
5 |
| -with Reactive Streams back pressure. It shares |
6 |
| -<<web-reactive.adoc#webflux-codecs,HTTP codecs>> and other infrastructure with the |
| 4 | +The `spring-webflux` module includes a reactive, non-blocking client for HTTP requests |
| 5 | +with a functional-style API client and Reactive Streams support. `WebClient` depends on a |
| 6 | +lower level HTTP client library to execute requests and that support is pluggable. |
| 7 | + |
| 8 | +`WebClient` |
| 9 | +uses the same <<web-reactive.adoc#webflux-codecs,codecs>> as WebFlux server applications do, and |
| 10 | +shares a common base package, some common APIs, and infrastructure with the |
7 | 11 | server <<web-reactive.adoc#webflux-fn,functional web framework>>.
|
| 12 | +The API exposes Reactor `Flux` and `Mono` types, also see |
| 13 | +<<web-reactive.adoc#webflux-reactive-libraries>>. By default it uses |
| 14 | +it uses https://github.com/reactor/reactor-netty[Reactor Netty] as the HTTP client |
| 15 | +library but others can be plugged in through a custom `ClientHttpConnector`. |
8 | 16 |
|
9 |
| -`WebClient` provides a higher level API over HTTP client libraries. By default |
10 |
| -it uses https://github.com/reactor/reactor-netty[Reactor Netty] but that is pluggable |
11 |
| -with a different `ClientHttpConnector`. The `WebClient` API returns Reactor `Flux` or |
12 |
| -`Mono` for output and accepts Reactive Streams `Publisher` as input (see |
13 |
| -<<web-reactive.adoc#webflux-reactive-libraries>>). |
| 17 | +By comparison to the <<integration.adoc#rest-resttemplate,RestTemplate>>, the |
| 18 | +`WebClient` is: |
14 | 19 |
|
15 |
| -[TIP] |
16 |
| -==== |
17 |
| -By comparison to the |
18 |
| -<<integration.adoc#rest-resttemplate,RestTemplate>>, the `WebClient` offers a more |
19 |
| -functional and fluent API that taking full advantage of Java 8 lambdas. It supports both |
20 |
| -sync and async scenarios, including streaming, and brings the efficiency of |
21 |
| -non-blocking I/O. |
22 |
| -==== |
| 20 | +* non-blocking, reactive, and supports higher concurrency with less hardware resources. |
| 21 | +* provides a functional API that takes advantage of Java 8 lambdas. |
| 22 | +* supports both synchronous and asynchronous scenarios. |
| 23 | +* supports streaming up or down from a server. |
| 24 | + |
| 25 | +For most concurrent scenarios, e.g. a sequence of possibly inter-dependent HTTP calls, |
| 26 | +or for making remote calls from the server-side, prefer using the `WebClient`. |
23 | 27 |
|
24 | 28 |
|
25 | 29 |
|
@@ -336,3 +340,17 @@ You can also mutate an existing `WebClient` instance without affecting the origi
|
336 | 340 | .filter(basicAuthentication("user", "pwd")
|
337 | 341 | .build();
|
338 | 342 | ----
|
| 343 | + |
| 344 | + |
| 345 | + |
| 346 | + |
| 347 | +[[webflux-client-testing]] |
| 348 | +== Testing |
| 349 | + |
| 350 | +To test code that uses the `WebClient`, you can use a mock web server such as the |
| 351 | +https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see example |
| 352 | +use, check |
| 353 | +https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java[WebClientIntegrationTests] |
| 354 | +in the Spring Framework tests, or the |
| 355 | +https://github.com/square/okhttp/tree/master/samples/static-server[static-server] |
| 356 | +sample in the OkHttp repository. |
0 commit comments