Provide WebClient#exchange() alternative for Coroutines

This commit adds awaitExchange { } and
exchangeToFlow { } extensions as Coroutines variants for
exchangeToMono() and exchangeToFlux().

Closes gh-25751
This commit is contained in:
Sébastien Deleuze
2020-10-07 10:58:57 +02:00
parent e899397438
commit 7d7ed88739
3 changed files with 70 additions and 5 deletions

View File

@@ -537,10 +537,10 @@ responses, use `onStatus` handlers as follows:
[[webflux-client-exchange]]
== `exchangeToMono()`
== Exchange
The `exchangeToMono()` and `exchangeToFlux()` methods are useful for more advanced
cases that require more control, such as to decode the response differently
The `exchangeToMono()` and `exchangeToFlux()` methods (or `awaitExchange { }` and `exchangeToFlow { }` in Kotlin)
are useful for more advanced cases that require more control, such as to decode the response differently
depending on the response status:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
@@ -564,6 +564,20 @@ depending on the response status:
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val entity = client.get()
.uri("/persons/1")
.accept(MediaType.APPLICATION_JSON)
.awaitExchange {
if (response.statusCode() == HttpStatus.OK) {
return response.awaitBody<Person>();
}
else if (response.statusCode().is4xxClientError) {
return response.awaitBody<ErrorContainer>();
}
else {
return response.createExceptionAndAwait();
}
}
----
When using the above, after the returned `Mono` or `Flux` completes, the response body