|
|
|
@ -115,10 +115,11 @@ implies a synchronous, non-blocking controller method.
@@ -115,10 +115,11 @@ implies a synchronous, non-blocking controller method.
|
|
|
|
|
|
|
|
|
|
Spring Framework 5 adds a new reactive `WebClient` in addition to the existing `RestTemplate`. |
|
|
|
|
|
|
|
|
|
Each supported HTTP client (e.g. Reactor Netty) is adapted to a set of shared, |
|
|
|
|
A `WebClient` instance use a `ClientHttpConnector` implementation to drive the underlying |
|
|
|
|
supported HTTP client (e.g. Reactor Netty). This client is adapted to a set of shared, |
|
|
|
|
reactive `ClientHttpRequest` and `ClientHttpResponse` abstractions that expose the request |
|
|
|
|
and response body as `Flux<DataBuffer>` with full backpressure support on the read and |
|
|
|
|
the write side. The `Encoder` and `Decoder` abstractions from `spring-core` are also used on |
|
|
|
|
the write side. The `HttpMessageReader` and `HttpMessageWriter` abstractions are also used on |
|
|
|
|
the client side for the serialization of a `Flux` of bytes to and from typed objects. |
|
|
|
|
|
|
|
|
|
An example of using the `WebClient`: |
|
|
|
@ -126,27 +127,17 @@ An example of using the `WebClient`:
@@ -126,27 +127,17 @@ An example of using the `WebClient`:
|
|
|
|
|
[source,java,indent=0] |
|
|
|
|
[subs="verbatim,quotes"] |
|
|
|
|
---- |
|
|
|
|
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(); |
|
|
|
|
WebClient webClient = new WebClient(httpConnector); |
|
|
|
|
// create an immutable instance of WebClient |
|
|
|
|
WebClient webClient = WebClient.create(new ReactorClientHttpConnector()); |
|
|
|
|
|
|
|
|
|
Mono<Account> response = webClient |
|
|
|
|
.perform(get("http://example.com/accounts/1").accept(APPLICATION_JSON)) |
|
|
|
|
.extract(body(Account.class)); |
|
|
|
|
---- |
|
|
|
|
|
|
|
|
|
The above assumes static method imports from `ClientWebRequestBuilders` and `ResponseExtractors` |
|
|
|
|
that enable a fluent syntax. The same can also be done with RxJava using static imports from |
|
|
|
|
`RxJava1ClientWebRequestBuilder` and `RxJava1ResponseExtractors` instead: |
|
|
|
|
ClientRequest<Void> request = ClientRequest.GET("http://example.com/accounts/{id}", 1L) |
|
|
|
|
.accept(MediaType.APPLICATION_JSON).build(); |
|
|
|
|
|
|
|
|
|
[source,java,indent=0] |
|
|
|
|
[subs="verbatim,quotes"] |
|
|
|
|
---- |
|
|
|
|
Single<Account> response = webClient |
|
|
|
|
.perform(get("http://example.com/accounts/1").accept(APPLICATION_JSON)) |
|
|
|
|
.extract(body(Account.class)); |
|
|
|
|
Mono<Account> account = this.webClient |
|
|
|
|
.exchange(request) |
|
|
|
|
.then(response -> response.body(toMono(Account.class))); |
|
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[web-reactive-getting-started]] |
|
|
|
|
== Getting Started |
|
|
|
|
|
|
|
|
|