Skip to content

Commit 1e19e51

Browse files
committed
Fix exchangeToMono sample in reference
Closes gh-26189
1 parent 69aed83 commit 1e19e51

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/docs/asciidoc/web/webflux-webclient.adoc

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -547,37 +547,39 @@ depending on the response status:
547547
.Java
548548
----
549549
Mono<Object> entityMono = client.get()
550-
.uri("/persons/1")
551-
.accept(MediaType.APPLICATION_JSON)
552-
.exchangeToMono(response -> {
553-
if (response.statusCode().equals(HttpStatus.OK)) {
554-
return response.bodyToMono(Person.class);
555-
}
556-
else if (response.statusCode().is4xxClientError()) {
557-
return response.bodyToMono(ErrorContainer.class);
558-
}
559-
else {
560-
return Mono.error(response.createException());
561-
}
562-
});
550+
.uri("/persons/1")
551+
.accept(MediaType.APPLICATION_JSON)
552+
.exchangeToMono(response -> {
553+
if (response.statusCode().equals(HttpStatus.OK)) {
554+
return response.bodyToMono(Person.class);
555+
}
556+
else if (response.statusCode().is4xxClientError()) {
557+
// Suppress error status code
558+
return response.bodyToMono(ErrorContainer.class);
559+
}
560+
else {
561+
// Turn to error
562+
return response.createException().flatMap(Mono::error);
563+
}
564+
});
563565
----
564566
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
565567
.Kotlin
566568
----
567-
val entity = client.get()
568-
.uri("/persons/1")
569-
.accept(MediaType.APPLICATION_JSON)
570-
.awaitExchange {
571-
if (response.statusCode() == HttpStatus.OK) {
572-
return response.awaitBody<Person>();
573-
}
574-
else if (response.statusCode().is4xxClientError) {
575-
return response.awaitBody<ErrorContainer>();
576-
}
577-
else {
578-
return response.createExceptionAndAwait();
579-
}
580-
}
569+
val entity = client.get()
570+
.uri("/persons/1")
571+
.accept(MediaType.APPLICATION_JSON)
572+
.awaitExchange {
573+
if (response.statusCode() == HttpStatus.OK) {
574+
return response.awaitBody<Person>()
575+
}
576+
else if (response.statusCode().is4xxClientError) {
577+
return response.awaitBody<ErrorContainer>()
578+
}
579+
else {
580+
throw response.createExceptionAndAwait()
581+
}
582+
}
581583
----
582584

583585
When using the above, after the returned `Mono` or `Flux` completes, the response body

0 commit comments

Comments
 (0)