Skip to content

Commit 5649a6f

Browse files
committed
Update exchangeToMono Javadoc
This time showing a more representative example. See gh-27645
1 parent be6eeaf commit 5649a6f

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,15 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
519519
* scenarios, for example to decode the response differently depending
520520
* on the response status:
521521
* <p><pre>
522-
* Mono&lt;Object&gt; entityMono = client.get()
522+
* Mono&lt;Person&gt; entityMono = client.get()
523523
* .uri("/persons/1")
524524
* .accept(MediaType.APPLICATION_JSON)
525525
* .exchangeToMono(response -&gt; {
526526
* if (response.statusCode().equals(HttpStatus.OK)) {
527527
* return response.bodyToMono(Person.class);
528528
* }
529-
* else if (response.statusCode().is4xxClientError()) {
530-
* return response.bodyToMono(ErrorContainer.class);
531-
* }
532529
* else {
533-
* return response.createException();
530+
* return response.createException().flatMap(Mono::error);
534531
* }
535532
* });
536533
* </pre>
@@ -551,18 +548,15 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
551548
* scenarios, for example to decode the response differently depending
552549
* on the response status:
553550
* <p><pre>
554-
* Mono&lt;Object&gt; entityMono = client.get()
551+
* Flux&lt;Person&gt; entityMono = client.get()
555552
* .uri("/persons")
556553
* .accept(MediaType.APPLICATION_JSON)
557554
* .exchangeToFlux(response -&gt; {
558555
* if (response.statusCode().equals(HttpStatus.OK)) {
559556
* return response.bodyToFlux(Person.class);
560557
* }
561-
* else if (response.statusCode().is4xxClientError()) {
562-
* return response.bodyToMono(ErrorContainer.class).flux();
563-
* }
564558
* else {
565-
* return response.createException().flux();
559+
* return response.createException().flatMapMany(Mono::error);
566560
* }
567561
* });
568562
* </pre>

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,20 +546,16 @@ depending on the response status:
546546
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
547547
.Java
548548
----
549-
Mono<Object> entityMono = client.get()
549+
Mono<Person> entityMono = client.get()
550550
.uri("/persons/1")
551551
.accept(MediaType.APPLICATION_JSON)
552552
.exchangeToMono(response -> {
553553
if (response.statusCode().equals(HttpStatus.OK)) {
554554
return response.bodyToMono(Person.class);
555555
}
556-
else if (response.statusCode().is4xxClientError()) {
557-
// Suppress error status code
558-
return response.bodyToMono(ErrorContainer.class);
559-
}
560556
else {
561557
// Turn to error
562-
return response.createException();
558+
return response.createException().flatMap(Mono::error);
563559
}
564560
});
565561
----
@@ -573,9 +569,6 @@ val entity = client.get()
573569
if (response.statusCode() == HttpStatus.OK) {
574570
return response.awaitBody<Person>()
575571
}
576-
else if (response.statusCode().is4xxClientError) {
577-
return response.awaitBody<ErrorContainer>()
578-
}
579572
else {
580573
throw response.createExceptionAndAwait()
581574
}

0 commit comments

Comments
 (0)