JDK HttpClient connector for WebClient

See gh-21014
This commit is contained in:
Julien Eyraud
2019-08-07 21:40:44 +02:00
committed by Rossen Stoyanchev
parent ab2b3d91fc
commit d930617442
6 changed files with 381 additions and 8 deletions

View File

@@ -14,6 +14,7 @@ support for the following:
* https://github.com/reactor/reactor-netty[Reactor Netty]
* https://github.com/jetty-project/jetty-reactive-httpclient[Jetty Reactive HttpClient]
* https://hc.apache.org/index.html[Apache HttpComponents]
* https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html[JDK HttpClient]
* Others can be plugged via `ClientHttpConnector`.
@@ -382,7 +383,7 @@ shows:
HttpClient httpClient = new HttpClient();
// Further customizations...
ClientHttpConnector connector =
new JettyClientHttpConnector(httpClient, resourceFactory()); <1>
@@ -403,7 +404,7 @@ shows:
val httpClient = HttpClient()
// Further customizations...
val connector = JettyClientHttpConnector(httpClient, resourceFactory()) // <1>
return WebClient.builder().clientConnector(connector).build() // <2>
@@ -439,6 +440,43 @@ The following example shows how to customize Apache HttpComponents `HttpClient`
val webClient = WebClient.builder().clientConnector(connector).build()
----
[[webflux-client-builder-jdk-httpclient]]
=== JDK
The following example shows how to customize JDK `HttpClient` settings:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
@Bean
public WebClient webClient() {
HttpClient httpClient = HttpClient.newBuilder()
.executor(...)
.connectTimeout(...)
.build();
ClientHttpConnector connector =
new JdkClientHttpConnector(httpClient, new DefaultDataBufferFactory()); // <1>
return WebClient webClient = WebClient.builder().clientConnector(connector).build(); // <2>
}
----
<1> Use the `JdkClientHttpConnector` constructor with customized `HttpClient` instance.
<2> Plug the connector into the `WebClient.Builder`.
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Bean
fun webClient() : WebClient {
val httpClient = HttpClient.newBuilder()
.executor(...)
.connectTimeout(...)
.build()
val connector = JdkClientHttpConnector(httpClient, DefaultDataBufferFactory()) // <1>
return WebClient webClient = WebClient.builder()
.clientConnector(connector).build() // <2>
}
----
[[webflux-client-retrieve]]
== `retrieve()`
@@ -761,9 +799,9 @@ multipart request. The following example shows how to create a `MultiValueMap<St
part("fieldPart", "fieldValue")
part("filePart1", new FileSystemResource("...logo.png"))
part("jsonPart", new Person("Jason"))
part("myPart", part) // Part from a server request
part("myPart", part) // Part from a server request
}
val parts = builder.build()
----
@@ -1063,7 +1101,7 @@ apply to all operations. For example:
client.get().uri("/person/{id}", i).retrieve()
.awaitBody<Person>()
}
val persons = runBlocking {
client.get().uri("/persons").retrieve()
.bodyToFlow<Person>()
@@ -1118,7 +1156,7 @@ inter-dependent, without ever blocking until the end.
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 .
controller method .
====