Fix RestClient generic type handling
For client side use case, the context class should be null, consistently with what is done in HttpMessageConverterExtractor. Closes gh-31574
This commit is contained in:
@@ -69,6 +69,7 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
* as created by the static factory methods.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
* @since 6.1
|
||||
* @see RestClient#create()
|
||||
* @see RestClient#create(String)
|
||||
@@ -609,11 +610,11 @@ final class DefaultRestClient implements RestClient {
|
||||
|
||||
for (HttpMessageConverter<?> messageConverter : DefaultRestClient.this.messageConverters) {
|
||||
if (messageConverter instanceof GenericHttpMessageConverter genericHttpMessageConverter) {
|
||||
if (genericHttpMessageConverter.canRead(bodyType, bodyClass, contentType)) {
|
||||
if (genericHttpMessageConverter.canRead(bodyType, null, contentType)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Reading to [" + ResolvableType.forType(bodyType) + "]");
|
||||
}
|
||||
return (T) genericHttpMessageConverter.read(bodyType, bodyClass, this.clientResponse);
|
||||
return (T) genericHttpMessageConverter.read(bodyType, null, this.clientResponse);
|
||||
}
|
||||
}
|
||||
if (messageConverter.canRead(bodyClass, contentType)) {
|
||||
|
||||
@@ -63,6 +63,7 @@ import static org.junit.jupiter.api.Named.named;
|
||||
* Integration tests for {@link RestClient}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class RestClientIntegrationTests {
|
||||
|
||||
@@ -179,6 +180,29 @@ class RestClientIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedRestClientTest
|
||||
void retrieveJsonWithListParameterizedTypeReference(ClientHttpRequestFactory requestFactory) {
|
||||
startServer(requestFactory);
|
||||
|
||||
String content = "{\"containerValue\":[{\"bar\":\"barbar\",\"foo\":\"foofoo\"}]}";
|
||||
prepareResponse(response -> response
|
||||
.setHeader("Content-Type", "application/json").setBody(content));
|
||||
|
||||
ValueContainer<List<Pojo>> result = this.restClient.get()
|
||||
.uri("/json").accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.body(new ParameterizedTypeReference<ValueContainer<List<Pojo>>>() {});
|
||||
|
||||
assertThat(result.containerValue).isNotNull();
|
||||
assertThat(result.containerValue).containsExactly(new Pojo("foofoo", "barbar"));
|
||||
|
||||
expectRequestCount(1);
|
||||
expectRequest(request -> {
|
||||
assertThat(request.getPath()).isEqualTo("/json");
|
||||
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo("application/json");
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedRestClientTest
|
||||
void retrieveJsonAsResponseEntity(ClientHttpRequestFactory requestFactory) {
|
||||
startServer(requestFactory);
|
||||
|
||||
Reference in New Issue
Block a user