diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 29e20a40e8..e0e65d02d7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -418,7 +418,7 @@ class DefaultWebClient implements WebClient { public Mono bodyToMono(ParameterizedTypeReference typeReference) { return this.responseMono.flatMap( response -> bodyToPublisher(response, BodyExtractors.toMono(typeReference), - mono -> (Mono)mono)); + this::monoThrowableToMono)); } private Mono monoThrowableToMono(Mono mono) { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index a8ebc56f52..43f85e6ef2 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -39,9 +39,7 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.codec.Pojo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; /** * Integration tests using a {@link ExchangeFunction} through {@link WebClient}. @@ -447,6 +445,28 @@ public class WebClientIntegrationTests { }); } + @Test + public void shouldApplyCustomStatusHandlerParameterizedTypeReference() throws Exception { + prepareResponse(response -> response.setResponseCode(500) + .setHeader("Content-Type", "text/plain").setBody("Internal Server error")); + + Mono result = this.webClient.get() + .uri("/greeting?name=Spring") + .retrieve() + .onStatus(HttpStatus::is5xxServerError, response -> Mono.just(new MyException("500 error!"))) + .bodyToMono(new ParameterizedTypeReference() {}); + + StepVerifier.create(result) + .expectError(MyException.class) + .verify(Duration.ofSeconds(3)); + + expectRequestCount(1); + expectRequest(request -> { + assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); + assertEquals("/greeting?name=Spring", request.getPath()); + }); + } + @Test public void shouldReceiveNotFoundEntity() throws Exception { prepareResponse(response -> response.setResponseCode(404)