@@ -547,37 +547,39 @@ depending on the response status:
547
547
.Java
548
548
----
549
549
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
+ });
563
565
----
564
566
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
565
567
.Kotlin
566
568
----
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
+ }
581
583
----
582
584
583
585
When using the above, after the returned `Mono` or `Flux` completes, the response body
0 commit comments