You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
2 years ago
|
[[webflux-client-exchange]]
|
||
|
= Exchange
|
||
|
|
||
|
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"]
|
||
|
.Java
|
||
|
----
|
||
|
Mono<Person> entityMono = client.get()
|
||
|
.uri("/persons/1")
|
||
|
.accept(MediaType.APPLICATION_JSON)
|
||
|
.exchangeToMono(response -> {
|
||
|
if (response.statusCode().equals(HttpStatus.OK)) {
|
||
|
return response.bodyToMono(Person.class);
|
||
|
}
|
||
|
else {
|
||
|
// Turn to error
|
||
|
return response.createError();
|
||
|
}
|
||
|
});
|
||
|
----
|
||
|
[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 {
|
||
|
throw response.createExceptionAndAwait()
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
When using the above, after the returned `Mono` or `Flux` completes, the response body
|
||
|
is checked and if not consumed it is released to prevent memory and connection leaks.
|
||
|
Therefore the response cannot be decoded further downstream. It is up to the provided
|
||
|
function to declare how to decode the response if needed.
|
||
|
|
||
|
|
||
|
|
||
|
|