Fix exchangeToMono sample in reference

Closes gh-26189
This commit is contained in:
Rossen Stoyanchev 2020-12-01 16:44:20 +00:00
parent 69aed83504
commit 1e19e51c9c
1 changed files with 29 additions and 27 deletions

View File

@ -547,37 +547,39 @@ depending on the response status:
.Java .Java
---- ----
Mono<Object> entityMono = client.get() Mono<Object> entityMono = client.get()
.uri("/persons/1") .uri("/persons/1")
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.exchangeToMono(response -> { .exchangeToMono(response -> {
if (response.statusCode().equals(HttpStatus.OK)) { if (response.statusCode().equals(HttpStatus.OK)) {
return response.bodyToMono(Person.class); return response.bodyToMono(Person.class);
} }
else if (response.statusCode().is4xxClientError()) { else if (response.statusCode().is4xxClientError()) {
return response.bodyToMono(ErrorContainer.class); // Suppress error status code
} return response.bodyToMono(ErrorContainer.class);
else { }
return Mono.error(response.createException()); else {
} // Turn to error
}); return response.createException().flatMap(Mono::error);
}
});
---- ----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin .Kotlin
---- ----
val entity = client.get() val entity = client.get()
.uri("/persons/1") .uri("/persons/1")
.accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)
.awaitExchange { .awaitExchange {
if (response.statusCode() == HttpStatus.OK) { if (response.statusCode() == HttpStatus.OK) {
return response.awaitBody<Person>(); return response.awaitBody<Person>()
} }
else if (response.statusCode().is4xxClientError) { else if (response.statusCode().is4xxClientError) {
return response.awaitBody<ErrorContainer>(); return response.awaitBody<ErrorContainer>()
} }
else { else {
return response.createExceptionAndAwait(); throw response.createExceptionAndAwait()
} }
} }
---- ----
When using the above, after the returned `Mono` or `Flux` completes, the response body When using the above, after the returned `Mono` or `Flux` completes, the response body