WebClient throws ClassCastException for bodyToMono(ParameterizedTypeReference)

Prior to this commit, the `WebClient` always throws a `ClassCastException`
when an error occurs in `bodyToMono(ParameterizedTypeReference)``, and
not the expected exception, as set up by `onStatus`

Issue: SPR-16025
This commit is contained in:
Arjen Poutsma 2017-09-29 10:15:40 +02:00
parent d332e06f6c
commit 69945f4185
2 changed files with 24 additions and 4 deletions

View File

@ -418,7 +418,7 @@ class DefaultWebClient implements WebClient {
public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) {
return this.responseMono.flatMap(
response -> bodyToPublisher(response, BodyExtractors.toMono(typeReference),
mono -> (Mono<T>)mono));
this::monoThrowableToMono));
}
private <T> Mono<T> monoThrowableToMono(Mono<? extends Throwable> mono) {

View File

@ -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<String> result = this.webClient.get()
.uri("/greeting?name=Spring")
.retrieve()
.onStatus(HttpStatus::is5xxServerError, response -> Mono.just(new MyException("500 error!")))
.bodyToMono(new ParameterizedTypeReference<String>() {});
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)