2017-09-28 11:34:45 +08:00
|
|
|
[[webflux-client]]
|
|
|
|
= WebClient
|
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
Spring WebFlux includes a reactive, non-blocking `WebClient` for HTTP requests. The client
|
|
|
|
has a functional, fluent API with reactive types for declarative composition, see
|
|
|
|
<<web-reactive.adoc#webflux-reactive-libraries>>. WebFlux client and server rely on the
|
2019-03-05 20:08:34 +08:00
|
|
|
same non-blocking <<web-reactive.adoc#webflux-codecs, codecs>> to encode and decode request
|
2018-09-20 22:37:31 +08:00
|
|
|
and response content.
|
2018-02-15 06:32:24 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
Internally `WebClient` delegates to an HTTP client library. By default, it uses
|
2018-08-14 20:48:14 +08:00
|
|
|
https://github.com/reactor/reactor-netty[Reactor Netty], there is built-in support for
|
2018-12-26 20:13:09 +08:00
|
|
|
the Jetty https://github.com/jetty-project/jetty-reactive-httpclient[reactive HttpClient],
|
2018-08-14 20:48:14 +08:00
|
|
|
and others can be plugged in through a `ClientHttpConnector`.
|
2017-09-28 11:34:45 +08:00
|
|
|
|
|
|
|
|
2018-02-15 06:32:24 +08:00
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2018-08-14 20:48:14 +08:00
|
|
|
[[webflux-client-builder]]
|
|
|
|
== Configuration
|
|
|
|
|
|
|
|
The simplest way to create a `WebClient` is through one of the static factory methods:
|
|
|
|
|
|
|
|
* `WebClient.create()`
|
|
|
|
* `WebClient.create(String baseUrl)`
|
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
The above methods use the Reactor Netty `HttpClient` with default settings and expect
|
|
|
|
`io.projectreactor.netty:reactor-netty` to be on the classpath.
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
You can also use `WebClient.builder()` with further options:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
* `uriBuilderFactory`: Customized `UriBuilderFactory` to use as a base URL.
|
|
|
|
* `defaultHeader`: Headers for every request.
|
2018-09-20 22:37:31 +08:00
|
|
|
* `defaultCookie`: Cookies for every request.
|
2018-09-17 22:36:43 +08:00
|
|
|
* `defaultRequest`: `Consumer` to customize every request.
|
|
|
|
* `filter`: Client filter for every request.
|
|
|
|
* `exchangeStrategies`: HTTP message reader/writer customizations.
|
|
|
|
* `clientConnector`: HTTP client library settings.
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-03-05 20:08:34 +08:00
|
|
|
The following example configures <<web-reactive.adoc#webflux-codecs, HTTP codecs>>:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
2019-12-02 17:39:53 +08:00
|
|
|
ExchangeStrategies strategies = ExchangeStrategies.builder()
|
|
|
|
.codecs(configurer -> {
|
|
|
|
// ...
|
|
|
|
})
|
|
|
|
.build();
|
2018-08-14 20:48:14 +08:00
|
|
|
|
|
|
|
WebClient client = WebClient.builder()
|
2019-12-02 17:39:53 +08:00
|
|
|
.exchangeStrategies(strategies)
|
2018-08-14 20:48:14 +08:00
|
|
|
.build();
|
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
2019-12-02 17:39:53 +08:00
|
|
|
val strategies = ExchangeStrategies.builder()
|
|
|
|
.codecs {
|
|
|
|
// ...
|
2019-08-28 17:18:26 +08:00
|
|
|
}
|
|
|
|
.build()
|
2019-12-02 17:39:53 +08:00
|
|
|
|
|
|
|
val client = WebClient.builder()
|
|
|
|
.exchangeStrategies(strategies)
|
|
|
|
.build()
|
2019-08-28 17:18:26 +08:00
|
|
|
----
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
Once built, a `WebClient` instance is immutable. However, you can clone it and build a
|
|
|
|
modified copy without affecting the original instance, as the following example shows:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
|
|
|
WebClient client1 = WebClient.builder()
|
|
|
|
.filter(filterA).filter(filterB).build();
|
|
|
|
|
|
|
|
WebClient client2 = client1.mutate()
|
|
|
|
.filter(filterC).filter(filterD).build();
|
|
|
|
|
|
|
|
// client1 has filterA, filterB
|
|
|
|
|
|
|
|
// client2 has filterA, filterB, filterC, filterD
|
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val client1 = WebClient.builder()
|
|
|
|
.filter(filterA).filter(filterB).build()
|
|
|
|
|
|
|
|
val client2 = client1.mutate()
|
|
|
|
.filter(filterC).filter(filterD).build()
|
|
|
|
|
|
|
|
// client1 has filterA, filterB
|
|
|
|
|
|
|
|
// client2 has filterA, filterB, filterC, filterD
|
|
|
|
----
|
2018-08-14 20:48:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[webflux-client-builder-reactor]]
|
|
|
|
=== Reactor Netty
|
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
To customize Reactor Netty settings, simple provide a pre-configured `HttpClient`:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
2018-08-16 22:39:19 +08:00
|
|
|
HttpClient httpClient = HttpClient.create().secure(sslSpec -> ...);
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
WebClient webClient = WebClient.builder()
|
|
|
|
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
|
|
|
.build();
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val httpClient = HttpClient.create().secure { ... }
|
|
|
|
|
|
|
|
val webClient = WebClient.builder()
|
|
|
|
.clientConnector(ReactorClientHttpConnector(httpClient))
|
|
|
|
.build()
|
|
|
|
----
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
|
|
|
|
[[webflux-client-builder-reactor-resources]]
|
|
|
|
==== Resources
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
By default, `HttpClient` participates in the global Reactor Netty resources held in
|
2018-08-14 20:48:14 +08:00
|
|
|
`reactor.netty.http.HttpResources`, including event loop threads and a connection pool.
|
2018-09-17 22:36:43 +08:00
|
|
|
This is the recommended mode, since fixed, shared resources are preferred for event loop
|
2018-08-14 20:48:14 +08:00
|
|
|
concurrency. In this mode global resources remain active until the process exits.
|
|
|
|
|
|
|
|
If the server is timed with the process, there is typically no need for an explicit
|
2018-09-17 22:36:43 +08:00
|
|
|
shutdown. However, if the server can start or stop in-process (for example, a Spring MVC
|
|
|
|
application deployed as a WAR), you can declare a Spring-managed bean of type
|
|
|
|
`ReactorResourceFactory` with `globalResources=true` (the default) to ensure that the Reactor
|
|
|
|
Netty global resources are shut down when the Spring `ApplicationContext` is closed,
|
|
|
|
as the following example shows:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
public ReactorResourceFactory reactorResourceFactory() {
|
|
|
|
return new ReactorResourceFactory();
|
|
|
|
}
|
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
fun reactorResourceFactory() = ReactorResourceFactory()
|
|
|
|
----
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also choose not to participate in the global Reactor Netty resources. However,
|
|
|
|
in this mode, the burden is on you to ensure that all Reactor Netty client and server
|
|
|
|
instances use shared resources, as the following example shows:
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-14 20:48:14 +08:00
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
public ReactorResourceFactory resourceFactory() {
|
|
|
|
ReactorResourceFactory factory = new ReactorResourceFactory();
|
2019-08-28 17:18:26 +08:00
|
|
|
factory.setUseGlobalResources(false); // <1>
|
2018-08-14 20:48:14 +08:00
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public WebClient webClient() {
|
|
|
|
|
|
|
|
Function<HttpClient, HttpClient> mapper = client -> {
|
|
|
|
// Further customizations...
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientHttpConnector connector =
|
2019-08-28 17:18:26 +08:00
|
|
|
new ReactorClientHttpConnector(resourceFactory(), mapper); // <2>
|
|
|
|
|
|
|
|
return WebClient.builder().clientConnector(connector).build(); // <3>
|
|
|
|
}
|
|
|
|
----
|
|
|
|
<1> Create resources independent of global ones.
|
|
|
|
<2> Use the `ReactorClientHttpConnector` constructor with resource factory.
|
|
|
|
<3> Plug the connector into the `WebClient.Builder`.
|
|
|
|
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
fun resourceFactory() = ReactorResourceFactory().apply {
|
|
|
|
isUseGlobalResources = false // <1>
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
fun webClient(): WebClient {
|
2018-08-14 20:48:14 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
val mapper: (HttpClient) -> HttpClient = {
|
|
|
|
// Further customizations...
|
|
|
|
}
|
|
|
|
|
|
|
|
val connector = ReactorClientHttpConnector(resourceFactory(), mapper) // <2>
|
|
|
|
|
|
|
|
return WebClient.builder().clientConnector(connector).build() // <3>
|
2018-08-14 20:48:14 +08:00
|
|
|
}
|
|
|
|
----
|
|
|
|
<1> Create resources independent of global ones.
|
2018-09-17 22:36:43 +08:00
|
|
|
<2> Use the `ReactorClientHttpConnector` constructor with resource factory.
|
2018-08-14 20:48:14 +08:00
|
|
|
<3> Plug the connector into the `WebClient.Builder`.
|
2018-09-17 22:36:43 +08:00
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-20 22:37:31 +08:00
|
|
|
[[webflux-client-builder-reactor-timeout]]
|
|
|
|
==== Timeouts
|
|
|
|
|
|
|
|
To configure a connection timeout:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
|
|
|
----
|
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
|
|
|
|
|
HttpClient httpClient = HttpClient.create()
|
|
|
|
.tcpConfiguration(client ->
|
|
|
|
client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000));
|
2018-09-20 22:37:31 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
import io.netty.channel.ChannelOption
|
2018-09-20 22:37:31 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
val httpClient = HttpClient.create()
|
|
|
|
.tcpConfiguration { it.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)}
|
2018-09-20 22:37:31 +08:00
|
|
|
----
|
|
|
|
|
|
|
|
To configure a read and/or write timeout values:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-09-20 22:37:31 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
import io.netty.handler.timeout.ReadTimeoutHandler;
|
|
|
|
import io.netty.handler.timeout.WriteTimeoutHandler;
|
2018-09-20 22:37:31 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
HttpClient httpClient = HttpClient.create()
|
|
|
|
.tcpConfiguration(client ->
|
|
|
|
client.doOnConnected(conn -> conn
|
|
|
|
.addHandlerLast(new ReadTimeoutHandler(10))
|
|
|
|
.addHandlerLast(new WriteTimeoutHandler(10))));
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
import io.netty.handler.timeout.ReadTimeoutHandler
|
|
|
|
import io.netty.handler.timeout.WriteTimeoutHandler
|
|
|
|
|
|
|
|
val httpClient = HttpClient.create().tcpConfiguration {
|
|
|
|
it.doOnConnected { conn -> conn
|
|
|
|
.addHandlerLast(ReadTimeoutHandler(10))
|
|
|
|
.addHandlerLast(WriteTimeoutHandler(10))
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 22:37:31 +08:00
|
|
|
----
|
|
|
|
|
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-08-16 22:34:36 +08:00
|
|
|
[[webflux-client-builder-jetty]]
|
|
|
|
=== Jetty
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
The following example shows how to customize Jetty `HttpClient` settings:
|
2018-08-16 22:34:36 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-16 22:34:36 +08:00
|
|
|
----
|
|
|
|
HttpClient httpClient = new HttpClient();
|
|
|
|
httpClient.setCookieStore(...);
|
|
|
|
ClientHttpConnector connector = new JettyClientHttpConnector(httpClient);
|
|
|
|
|
|
|
|
WebClient webClient = WebClient.builder().clientConnector(connector).build();
|
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val httpClient = HttpClient()
|
|
|
|
httpClient.cookieStore = ...
|
|
|
|
val connector = JettyClientHttpConnector(httpClient)
|
|
|
|
|
|
|
|
val webClient = WebClient.builder().clientConnector(connector).build();
|
|
|
|
----
|
2018-08-16 22:34:36 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
By default, `HttpClient` creates its own resources (`Executor`, `ByteBufferPool`, `Scheduler`),
|
2018-08-16 22:34:36 +08:00
|
|
|
which remain active until the process exits or `stop()` is called.
|
|
|
|
|
2018-09-21 22:33:04 +08:00
|
|
|
You can share resources between multiple instances of the Jetty client (and server) and
|
|
|
|
ensure that the resources are shut down when the Spring `ApplicationContext` is closed by
|
|
|
|
declaring a Spring-managed bean of type `JettyResourceFactory`, as the following example
|
|
|
|
shows:
|
2018-08-16 22:34:36 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-08-16 22:34:36 +08:00
|
|
|
----
|
|
|
|
@Bean
|
2018-09-23 19:30:17 +08:00
|
|
|
public JettyResourceFactory resourceFactory() {
|
2018-08-16 22:34:36 +08:00
|
|
|
return new JettyResourceFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public WebClient webClient() {
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
HttpClient httpClient = new HttpClient();
|
|
|
|
// Further customizations...
|
|
|
|
|
2018-08-16 22:34:36 +08:00
|
|
|
ClientHttpConnector connector =
|
2019-08-28 17:18:26 +08:00
|
|
|
new JettyClientHttpConnector(httpClient, resourceFactory()); <1>
|
2018-08-16 22:34:36 +08:00
|
|
|
|
2018-09-23 19:30:17 +08:00
|
|
|
return WebClient.builder().clientConnector(connector).build(); <2>
|
2018-08-16 22:34:36 +08:00
|
|
|
}
|
|
|
|
----
|
2018-09-22 05:06:09 +08:00
|
|
|
<1> Use the `JettyClientHttpConnector` constructor with resource factory.
|
|
|
|
<2> Plug the connector into the `WebClient.Builder`.
|
2018-08-16 22:34:36 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
@Bean
|
|
|
|
fun resourceFactory() = JettyResourceFactory()
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
fun webClient(): WebClient {
|
|
|
|
|
|
|
|
val httpClient = HttpClient()
|
|
|
|
// Further customizations...
|
|
|
|
|
|
|
|
val connector = JettyClientHttpConnector(httpClient, resourceFactory()) // <1>
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
return WebClient.builder().clientConnector(connector).build() // <2>
|
|
|
|
}
|
|
|
|
----
|
|
|
|
<1> Use the `JettyClientHttpConnector` constructor with resource factory.
|
|
|
|
<2> Plug the connector into the `WebClient.Builder`.
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
[[webflux-client-retrieve]]
|
2018-10-06 03:54:38 +08:00
|
|
|
== `retrieve()`
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
The `retrieve()` method is the easiest way to get a response body and decode it.
|
|
|
|
The following example shows how to do so:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-03-21 06:48:14 +08:00
|
|
|
WebClient client = WebClient.create("https://example.org");
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Person> result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Person.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val client = WebClient.create("https://example.org")
|
|
|
|
|
|
|
|
val result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Person>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also get a stream of objects decoded from the response, as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Flux<Quote> result = client.get()
|
|
|
|
.uri("/quotes").accept(MediaType.TEXT_EVENT_STREAM)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToFlux(Quote.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val result = client.get()
|
|
|
|
.uri("/quotes").accept(MediaType.TEXT_EVENT_STREAM)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToFlow<Quote>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-08-08 19:07:01 +08:00
|
|
|
By default, responses with 4xx or 5xx status codes result in an
|
2018-09-17 22:36:43 +08:00
|
|
|
`WebClientResponseException` or one of its HTTP status specific sub-classes, such as
|
2018-08-08 19:07:01 +08:00
|
|
|
`WebClientResponseException.BadRequest`, `WebClientResponseException.NotFound`, and others.
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also use the `onStatus` method to customize the resulting exception,
|
|
|
|
as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Person> result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.retrieve()
|
2019-01-14 17:49:11 +08:00
|
|
|
.onStatus(HttpStatus::is4xxClientError, response -> ...)
|
2017-11-21 05:28:00 +08:00
|
|
|
.onStatus(HttpStatus::is5xxServerError, response -> ...)
|
|
|
|
.bodyToMono(Person.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.retrieve()
|
|
|
|
.onStatus(HttpStatus::is4xxClientError) { ... }
|
|
|
|
.onStatus(HttpStatus::is5xxServerError) { ... }
|
|
|
|
.awaitBody<Person>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-11-09 02:26:41 +08:00
|
|
|
When `onStatus` is used, if the response is expected to have content, then the `onStatus`
|
|
|
|
callback should consume it. If not, the content will be automatically drained to ensure
|
|
|
|
resources are released.
|
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
[[webflux-client-exchange]]
|
2018-10-06 03:54:38 +08:00
|
|
|
== `exchange()`
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
The `exchange()` method provides more control than the `retrieve` method. The following example is equivalent
|
2017-09-28 11:34:45 +08:00
|
|
|
to `retrieve()` but also provides access to the `ClientResponse`:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Person> result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.exchange()
|
|
|
|
.flatMap(response -> response.bodyToMono(Person.class));
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.awaitExchange()
|
|
|
|
.awaitBody<Person>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
At this level, you can also create a full `ResponseEntity`:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<ResponseEntity<Person>> result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.exchange()
|
|
|
|
.flatMap(response -> response.toEntity(Person.class));
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val result = client.get()
|
|
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
|
|
.awaitExchange()
|
|
|
|
.toEntity<Person>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
Note that (unlike `retrieve()`), with `exchange()`, there are no automatic error signals for
|
2017-09-28 11:34:45 +08:00
|
|
|
4xx and 5xx responses. You have to check the status code and decide how to proceed.
|
|
|
|
|
2019-09-03 16:49:39 +08:00
|
|
|
[CAUTION]
|
|
|
|
====
|
|
|
|
When using `exchange()`, you have to make sure that the body is always consumed or released,
|
|
|
|
even when an exception occurs (see <<core.adoc#databuffers-using,Using DataBuffer>>).
|
|
|
|
Typically, you do this by invoking either `bodyTo*` or `toEntity*` on `ClientResponse`
|
|
|
|
to convert the body into an object of the desired type, but
|
|
|
|
you can also invoke `releaseBody()` to discard the body contents without consuming it or
|
|
|
|
`toBodilessEntity()` to get just the status and headers (while discarding the body).
|
|
|
|
|
|
|
|
Finally, there is `bodyToMono(Void.class)`, which should only be used if no response content is
|
|
|
|
expected.
|
|
|
|
If the response does have content, the connection is closed and is not placed back in the pool,
|
|
|
|
because it is not left in a reusable state.
|
|
|
|
====
|
2017-10-19 02:24:17 +08:00
|
|
|
|
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
[[webflux-client-body]]
|
2018-09-17 22:36:43 +08:00
|
|
|
== Request Body
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-07-08 03:03:41 +08:00
|
|
|
The request body can be encoded from any asynchronous type handled by `ReactiveAdapterRegistry`,
|
2019-08-28 17:18:26 +08:00
|
|
|
like `Mono` or Kotlin Coroutines `Deferred` as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Person> personMono = ... ;
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
|
|
.body(personMono, Person.class)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val personDeferred: Deferred<Person> = ...
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_JSON)
|
2019-08-28 18:15:55 +08:00
|
|
|
.body<Person>(personDeferred)
|
2019-08-28 17:18:26 +08:00
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also have a stream of objects be encoded, as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Flux<Person> personFlux = ... ;
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_STREAM_JSON)
|
|
|
|
.body(personFlux, Person.class)
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val people: Flow<Person> = ...
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_JSON)
|
2019-08-28 18:15:55 +08:00
|
|
|
.body(people)
|
2019-08-28 17:18:26 +08:00
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
Alternatively, if you have the actual value, you can use the `bodyValue` shortcut method,
|
2018-09-17 22:36:43 +08:00
|
|
|
as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
Person person = ... ;
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_JSON)
|
2019-08-28 17:18:26 +08:00
|
|
|
.bodyValue(person)
|
2017-11-21 05:28:00 +08:00
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val person: Person = ...
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/persons/{id}", id)
|
|
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
|
|
.bodyValue(person)
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-09-28 11:34:45 +08:00
|
|
|
|
|
|
|
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2017-11-07 05:28:27 +08:00
|
|
|
[[webflux-client-body-form]]
|
2018-09-17 22:36:43 +08:00
|
|
|
=== Form Data
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
To send form data, you can provide a `MultiValueMap<String, String>` as the body. Note that the
|
|
|
|
content is automatically set to `application/x-www-form-urlencoded` by the
|
|
|
|
`FormHttpMessageWriter`. The following example shows how to use `MultiValueMap<String, String>`:
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
MultiValueMap<String, String> formData = ... ;
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/path", id)
|
2019-08-28 17:18:26 +08:00
|
|
|
.bodyValue(formData)
|
2017-11-21 05:28:00 +08:00
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val formData: MultiValueMap<String, String> = ...
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.bodyValue(formData)
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also supply form data in-line by using `BodyInserters`, as the following example shows:
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
import static org.springframework.web.reactive.function.BodyInserters.*;
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.body(fromFormData("k1", "v1").with("k2", "v2"))
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
import org.springframework.web.reactive.function.BodyInserters.*
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.body(fromFormData("k1", "v1").with("k2", "v2"))
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-11-07 05:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[webflux-client-body-multipart]]
|
2018-09-17 22:36:43 +08:00
|
|
|
=== Multipart Data
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2018-03-24 07:00:59 +08:00
|
|
|
To send multipart data, you need to provide a `MultiValueMap<String, ?>` whose values are
|
2018-09-17 22:36:43 +08:00
|
|
|
either `Object` instances that represent part content or `HttpEntity` instances that represent the content and
|
2018-03-24 07:00:59 +08:00
|
|
|
headers for a part. `MultipartBodyBuilder` provides a convenient API to prepare a
|
2018-09-17 22:36:43 +08:00
|
|
|
multipart request. The following example shows how to create a `MultiValueMap<String, ?>`:
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
MultipartBodyBuilder builder = new MultipartBodyBuilder();
|
|
|
|
builder.part("fieldPart", "fieldValue");
|
2019-06-05 05:21:39 +08:00
|
|
|
builder.part("filePart1", new FileSystemResource("...logo.png"));
|
2017-11-21 05:28:00 +08:00
|
|
|
builder.part("jsonPart", new Person("Jason"));
|
2019-06-05 05:21:39 +08:00
|
|
|
builder.part("myPart", part); // Part from a server request
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
|
2018-03-24 07:00:59 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val builder = MultipartBodyBuilder().apply {
|
|
|
|
part("fieldPart", "fieldValue")
|
|
|
|
part("filePart1", new FileSystemResource("...logo.png"))
|
|
|
|
part("jsonPart", new Person("Jason"))
|
|
|
|
part("myPart", part) // Part from a server request
|
|
|
|
}
|
|
|
|
|
|
|
|
val parts = builder.build()
|
|
|
|
----
|
2018-03-24 07:00:59 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
In most cases, you do not have to specify the `Content-Type` for each part. The content
|
|
|
|
type is determined automatically based on the `HttpMessageWriter` chosen to serialize it
|
|
|
|
or, in the case of a `Resource`, based on the file extension. If necessary, you can
|
|
|
|
explicitly provide the `MediaType` to use for each part through one of the overloaded
|
2018-03-24 07:00:59 +08:00
|
|
|
builder `part` methods.
|
|
|
|
|
2019-11-27 10:08:36 +08:00
|
|
|
Once a `MultiValueMap` is prepared, the easiest way to pass it to the `WebClient` is
|
2019-07-08 03:03:41 +08:00
|
|
|
through the `body` method, as the following example shows:
|
2018-03-24 07:00:59 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-03-24 07:00:59 +08:00
|
|
|
----
|
|
|
|
MultipartBodyBuilder builder = ...;
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/path", id)
|
2019-07-08 03:03:41 +08:00
|
|
|
.body(builder.build())
|
2017-11-21 05:28:00 +08:00
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val builder: MultipartBodyBuilder = ...
|
|
|
|
|
|
|
|
client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.body(builder.build())
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
If the `MultiValueMap` contains at least one non-`String` value, which could also
|
|
|
|
represent regular form data (that is, `application/x-www-form-urlencoded`), you need not
|
|
|
|
set the `Content-Type` to `multipart/form-data`. This is always the case when using
|
|
|
|
`MultipartBodyBuilder`, which ensures an `HttpEntity` wrapper.
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2018-03-24 07:00:59 +08:00
|
|
|
As an alternative to `MultipartBodyBuilder`, you can also provide multipart content,
|
2018-09-17 22:36:43 +08:00
|
|
|
inline-style, through the built-in `BodyInserters`, as the following example shows:
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2017-11-21 05:28:00 +08:00
|
|
|
import static org.springframework.web.reactive.function.BodyInserters.*;
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2017-11-21 05:28:00 +08:00
|
|
|
Mono<Void> result = client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.body(fromMultipartData("fieldPart", "value").with("filePart", resource))
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2017-11-07 05:28:27 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
import org.springframework.web.reactive.function.BodyInserters.*
|
2017-11-07 05:28:27 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
client.post()
|
|
|
|
.uri("/path", id)
|
|
|
|
.body(fromMultipartData("fieldPart", "value").with("filePart", resource))
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
|
|
|
----
|
2017-10-19 02:24:17 +08:00
|
|
|
|
2018-03-24 07:00:59 +08:00
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2017-09-28 11:34:45 +08:00
|
|
|
[[webflux-client-filter]]
|
2018-05-24 19:16:54 +08:00
|
|
|
== Client Filters
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2018-08-14 20:48:14 +08:00
|
|
|
You can register a client filter (`ExchangeFilterFunction`) through the `WebClient.Builder`
|
2018-09-17 22:36:43 +08:00
|
|
|
in order to intercept and modify requests, as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
WebClient client = WebClient.builder()
|
|
|
|
.filter((request, next) -> {
|
2018-05-24 19:16:54 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
ClientRequest filtered = ClientRequest.from(request)
|
|
|
|
.header("foo", "bar")
|
|
|
|
.build();
|
2018-05-24 19:16:54 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
return next.exchange(filtered);
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val client = WebClient.builder()
|
|
|
|
.filter { request, next ->
|
|
|
|
|
|
|
|
val filtered = ClientRequest.from(request)
|
|
|
|
.header("foo", "bar")
|
|
|
|
.build()
|
|
|
|
|
|
|
|
next.exchange(filtered)
|
|
|
|
}
|
|
|
|
.build()
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
This can be used for cross-cutting concerns, such as authentication. The following example uses
|
2018-05-24 19:16:54 +08:00
|
|
|
a filter for basic authentication through a static factory method:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
WebClient client = WebClient.builder()
|
|
|
|
.filter(basicAuthentication("user", "password"))
|
|
|
|
.build();
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
val client = WebClient.builder()
|
|
|
|
.filter(basicAuthentication("user", "password"))
|
|
|
|
.build()
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
Filters apply globally to every request. To change a filter's behavior for a specific
|
2018-05-24 19:16:54 +08:00
|
|
|
request, you can add request attributes to the `ClientRequest` that can then be accessed
|
2018-09-17 22:36:43 +08:00
|
|
|
by all filters in the chain, as the following example shows:
|
2017-09-28 11:34:45 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
WebClient client = WebClient.builder()
|
|
|
|
.filter((request, next) -> {
|
|
|
|
Optional<Object> usr = request.attribute("myAttribute");
|
|
|
|
// ...
|
|
|
|
})
|
|
|
|
.build();
|
2018-05-24 19:16:54 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
client.get().uri("https://example.org/")
|
|
|
|
.attribute("myAttribute", "...")
|
|
|
|
.retrieve()
|
|
|
|
.bodyToMono(Void.class);
|
2018-05-24 19:16:54 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
}
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val client = WebClient.builder()
|
|
|
|
.filter { request, _ ->
|
|
|
|
val usr = request.attributes()["myAttribute"];
|
|
|
|
// ...
|
|
|
|
}.build()
|
|
|
|
|
|
|
|
client.get().uri("https://example.org/")
|
|
|
|
.attribute("myAttribute", "...")
|
|
|
|
.retrieve()
|
|
|
|
.awaitBody<Unit>()
|
2018-05-24 19:16:54 +08:00
|
|
|
----
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
You can also replicate an existing `WebClient`, insert new filters, or remove already
|
|
|
|
registered filters. The following example, inserts a basic authentication filter at
|
2018-05-24 19:16:54 +08:00
|
|
|
index 0:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2018-05-24 19:16:54 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
2018-05-24 19:16:54 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
WebClient client = webClient.mutate()
|
|
|
|
.filters(filterList -> {
|
|
|
|
filterList.add(0, basicAuthentication("user", "password"));
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val client = webClient.mutate()
|
|
|
|
.filters { it.add(0, basicAuthentication("user", "password")) }
|
|
|
|
.build()
|
2017-09-28 11:34:45 +08:00
|
|
|
----
|
2018-02-15 06:32:24 +08:00
|
|
|
|
|
|
|
|
2018-10-25 21:15:58 +08:00
|
|
|
|
2019-01-19 06:46:16 +08:00
|
|
|
[[webflux-client-synchronous]]
|
|
|
|
== Synchronous Use
|
|
|
|
|
|
|
|
`WebClient` can be used in synchronous style by blocking at the end for the result:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2019-01-19 06:46:16 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
Person person = client.get().uri("/person/{id}", i).retrieve()
|
|
|
|
.bodyToMono(Person.class)
|
|
|
|
.block();
|
2019-01-19 06:46:16 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
List<Person> persons = client.get().uri("/persons").retrieve()
|
|
|
|
.bodyToFlux(Person.class)
|
|
|
|
.collectList()
|
|
|
|
.block();
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val person = runBlocking {
|
|
|
|
client.get().uri("/person/{id}", i).retrieve()
|
|
|
|
.awaitBody<Person>()
|
|
|
|
}
|
|
|
|
|
|
|
|
val persons = runBlocking {
|
|
|
|
client.get().uri("/persons").retrieve()
|
|
|
|
.bodyToFlow<Person>()
|
|
|
|
.toList()
|
|
|
|
}
|
2019-01-19 06:46:16 +08:00
|
|
|
----
|
|
|
|
|
|
|
|
However if multiple calls need to be made, it's more efficient to avoid blocking on each
|
|
|
|
response individually, and instead wait for the combined result:
|
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
|
|
|
.Java
|
2019-01-19 06:46:16 +08:00
|
|
|
----
|
2019-08-28 17:18:26 +08:00
|
|
|
Mono<Person> personMono = client.get().uri("/person/{id}", personId)
|
|
|
|
.retrieve().bodyToMono(Person.class);
|
2019-01-19 06:46:16 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
Mono<List<Hobby>> hobbiesMono = client.get().uri("/person/{id}/hobbies", personId)
|
|
|
|
.retrieve().bodyToFlux(Hobby.class).collectList();
|
2019-01-19 06:46:16 +08:00
|
|
|
|
2019-08-28 17:18:26 +08:00
|
|
|
Map<String, Object> data = Mono.zip(personMono, hobbiesMono, (person, hobbies) -> {
|
|
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
|
|
map.put("person", person);
|
|
|
|
map.put("hobbies", hobbies);
|
|
|
|
return map;
|
|
|
|
})
|
|
|
|
.block();
|
|
|
|
----
|
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
|
|
|
.Kotlin
|
|
|
|
----
|
|
|
|
val data = runBlocking {
|
|
|
|
val personDeferred = async {
|
|
|
|
client.get().uri("/person/{id}", personId)
|
|
|
|
.retrieve().awaitBody<Person>()
|
|
|
|
}
|
|
|
|
|
|
|
|
val hobbiesDeferred = async {
|
|
|
|
client.get().uri("/person/{id}/hobbies", personId)
|
|
|
|
.retrieve().bodyToFlow<Hobby>().toList()
|
|
|
|
}
|
|
|
|
|
|
|
|
mapOf("person" to personDeferred.await(), "hobbies" to hobbiesDeferred.await())
|
|
|
|
}
|
2019-01-19 06:46:16 +08:00
|
|
|
----
|
|
|
|
|
|
|
|
The above is merely one example. There are lots of other patterns and operators for putting
|
|
|
|
together a reactive pipeline that makes many remote calls, potentially some nested,
|
|
|
|
inter-dependent, without ever blocking until the end.
|
|
|
|
|
|
|
|
[NOTE]
|
|
|
|
====
|
2019-08-28 17:18:26 +08:00
|
|
|
With `Flux` or `Mono`, you should never have to block in a Spring MVC or Spring WebFlux controller.
|
|
|
|
Simply return the resulting reactive type from the controller method. The same principle apply to
|
|
|
|
Kotlin Coroutines and Spring WebFlux, just use suspending function or return `Flow` in your
|
|
|
|
controller method .
|
2019-01-19 06:46:16 +08:00
|
|
|
====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-15 06:32:24 +08:00
|
|
|
[[webflux-client-testing]]
|
|
|
|
== Testing
|
|
|
|
|
2018-09-17 22:36:43 +08:00
|
|
|
To test code that uses the `WebClient`, you can use a mock web server, such as the
|
|
|
|
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see an example
|
2018-09-23 19:30:17 +08:00
|
|
|
of its use, check out
|
2018-09-17 22:36:43 +08:00
|
|
|
https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java[`WebClientIntegrationTests`]
|
2018-09-23 19:30:17 +08:00
|
|
|
in the Spring Framework test suite or the
|
2018-09-17 22:36:43 +08:00
|
|
|
https://github.com/square/okhttp/tree/master/samples/static-server[`static-server`]
|
2018-02-15 06:32:24 +08:00
|
|
|
sample in the OkHttp repository.
|