From 5649a6f8ef335735480616cbeb4dd87efb371adf Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 3 Dec 2021 09:21:21 +0000 Subject: [PATCH] Update exchangeToMono Javadoc This time showing a more representative example. See gh-27645 --- .../web/reactive/function/client/WebClient.java | 14 ++++---------- src/docs/asciidoc/web/webflux-webclient.adoc | 11 ++--------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java index 9097000ccc..1df8c06ffb 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java @@ -519,18 +519,15 @@ public interface WebClient { * scenarios, for example to decode the response differently depending * on the response status: *

-		 * Mono<Object> entityMono = client.get()
+		 * 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 if (response.statusCode().is4xxClientError()) {
-		 *             return response.bodyToMono(ErrorContainer.class);
-		 *         }
 		 *         else {
-		 *             return response.createException();
+		 *             return response.createException().flatMap(Mono::error);
 		 *         }
 		 *     });
 		 * 
@@ -551,18 +548,15 @@ public interface WebClient { * scenarios, for example to decode the response differently depending * on the response status: *

-		 * Mono<Object> entityMono = client.get()
+		 * Flux<Person> entityMono = client.get()
 		 *     .uri("/persons")
 		 *     .accept(MediaType.APPLICATION_JSON)
 		 *     .exchangeToFlux(response -> {
 		 *         if (response.statusCode().equals(HttpStatus.OK)) {
 		 *             return response.bodyToFlux(Person.class);
 		 *         }
-		 *         else if (response.statusCode().is4xxClientError()) {
-		 *             return response.bodyToMono(ErrorContainer.class).flux();
-		 *         }
 		 *         else {
-		 *             return response.createException().flux();
+		 *             return response.createException().flatMapMany(Mono::error);
 		 *         }
 		 *     });
 		 * 
diff --git a/src/docs/asciidoc/web/webflux-webclient.adoc b/src/docs/asciidoc/web/webflux-webclient.adoc index 3c2d18c561..e93d121404 100644 --- a/src/docs/asciidoc/web/webflux-webclient.adoc +++ b/src/docs/asciidoc/web/webflux-webclient.adoc @@ -546,20 +546,16 @@ depending on the response status: [source,java,indent=0,subs="verbatim,quotes",role="primary"] .Java ---- - Mono entityMono = client.get() + Mono entityMono = client.get() .uri("/persons/1") .accept(MediaType.APPLICATION_JSON) .exchangeToMono(response -> { if (response.statusCode().equals(HttpStatus.OK)) { return response.bodyToMono(Person.class); } - else if (response.statusCode().is4xxClientError()) { - // Suppress error status code - return response.bodyToMono(ErrorContainer.class); - } else { // Turn to error - return response.createException(); + return response.createException().flatMap(Mono::error); } }); ---- @@ -573,9 +569,6 @@ val entity = client.get() if (response.statusCode() == HttpStatus.OK) { return response.awaitBody() } - else if (response.statusCode().is4xxClientError) { - return response.awaitBody() - } else { throw response.createExceptionAndAwait() }