Skip to content

Commit 38724a1

Browse files
committed
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
1 parent 55d13a0 commit 38724a1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* as created by the static factory methods.
7070
*
7171
* @author Arjen Poutsma
72+
* @author Sebastien Deleuze
7273
* @since 6.1
7374
* @see RestClient#create()
7475
* @see RestClient#create(String)
@@ -609,11 +610,11 @@ private <T> T readWithMessageConverters(Type bodyType, Class<T> bodyClass) {
609610

610611
for (HttpMessageConverter<?> messageConverter : DefaultRestClient.this.messageConverters) {
611612
if (messageConverter instanceof GenericHttpMessageConverter genericHttpMessageConverter) {
612-
if (genericHttpMessageConverter.canRead(bodyType, bodyClass, contentType)) {
613+
if (genericHttpMessageConverter.canRead(bodyType, null, contentType)) {
613614
if (logger.isDebugEnabled()) {
614615
logger.debug("Reading to [" + ResolvableType.forType(bodyType) + "]");
615616
}
616-
return (T) genericHttpMessageConverter.read(bodyType, bodyClass, this.clientResponse);
617+
return (T) genericHttpMessageConverter.read(bodyType, null, this.clientResponse);
617618
}
618619
}
619620
if (messageConverter.canRead(bodyClass, contentType)) {

spring-web/src/test/java/org/springframework/web/client/RestClientIntegrationTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* Integration tests for {@link RestClient}.
6464
*
6565
* @author Arjen Poutsma
66+
* @author Sebastien Deleuze
6667
*/
6768
class RestClientIntegrationTests {
6869

@@ -179,6 +180,29 @@ void retrieveJsonWithParameterizedTypeReference(ClientHttpRequestFactory request
179180
});
180181
}
181182

183+
@ParameterizedRestClientTest
184+
void retrieveJsonWithListParameterizedTypeReference(ClientHttpRequestFactory requestFactory) {
185+
startServer(requestFactory);
186+
187+
String content = "{\"containerValue\":[{\"bar\":\"barbar\",\"foo\":\"foofoo\"}]}";
188+
prepareResponse(response -> response
189+
.setHeader("Content-Type", "application/json").setBody(content));
190+
191+
ValueContainer<List<Pojo>> result = this.restClient.get()
192+
.uri("/json").accept(MediaType.APPLICATION_JSON)
193+
.retrieve()
194+
.body(new ParameterizedTypeReference<ValueContainer<List<Pojo>>>() {});
195+
196+
assertThat(result.containerValue).isNotNull();
197+
assertThat(result.containerValue).containsExactly(new Pojo("foofoo", "barbar"));
198+
199+
expectRequestCount(1);
200+
expectRequest(request -> {
201+
assertThat(request.getPath()).isEqualTo("/json");
202+
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo("application/json");
203+
});
204+
}
205+
182206
@ParameterizedRestClientTest
183207
void retrieveJsonAsResponseEntity(ClientHttpRequestFactory requestFactory) {
184208
startServer(requestFactory);

0 commit comments

Comments
 (0)