@@ -184,32 +184,40 @@ public Builder mutate() {
184
184
return new DefaultRestClientBuilder (this .builder );
185
185
}
186
186
187
+ @ Nullable
187
188
@ SuppressWarnings ({"rawtypes" , "unchecked" })
188
- private <T > T readWithMessageConverters (ClientHttpResponse clientResponse , Runnable callback , Type bodyType , Class <T > bodyClass ) {
189
+ private <T > T readWithMessageConverters (ClientHttpResponse clientResponse , Runnable callback , Type bodyType ,
190
+ Class <T > bodyClass ) {
191
+
189
192
MediaType contentType = getContentType (clientResponse );
190
193
191
194
try (clientResponse ) {
192
195
callback .run ();
193
196
197
+ IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse (clientResponse );
198
+ if (!responseWrapper .hasMessageBody () || responseWrapper .hasEmptyMessageBody ()) {
199
+ return null ;
200
+ }
201
+
194
202
for (HttpMessageConverter <?> messageConverter : this .messageConverters ) {
195
203
if (messageConverter instanceof GenericHttpMessageConverter genericHttpMessageConverter ) {
196
204
if (genericHttpMessageConverter .canRead (bodyType , null , contentType )) {
197
205
if (logger .isDebugEnabled ()) {
198
206
logger .debug ("Reading to [" + ResolvableType .forType (bodyType ) + "]" );
199
207
}
200
- return (T ) genericHttpMessageConverter .read (bodyType , null , clientResponse );
208
+ return (T ) genericHttpMessageConverter .read (bodyType , null , responseWrapper );
201
209
}
202
210
}
203
211
if (messageConverter .canRead (bodyClass , contentType )) {
204
212
if (logger .isDebugEnabled ()) {
205
213
logger .debug ("Reading to [" + bodyClass .getName () + "] as \" " + contentType + "\" " );
206
214
}
207
- return (T ) messageConverter .read ((Class )bodyClass , clientResponse );
215
+ return (T ) messageConverter .read ((Class )bodyClass , responseWrapper );
208
216
}
209
217
}
210
218
throw new UnknownContentTypeException (bodyType , contentType ,
211
- clientResponse .getStatusCode (), clientResponse .getStatusText (),
212
- clientResponse .getHeaders (), RestClientUtils .getBody (clientResponse ));
219
+ responseWrapper .getStatusCode (), responseWrapper .getStatusText (),
220
+ responseWrapper .getHeaders (), RestClientUtils .getBody (responseWrapper ));
213
221
}
214
222
catch (UncheckedIOException | IOException | HttpMessageNotReadableException ex ) {
215
223
throw new RestClientException ("Error while extracting response for type [" +
@@ -585,11 +593,13 @@ private ResponseSpec onStatusInternal(StatusHandler statusHandler) {
585
593
}
586
594
587
595
@ Override
596
+ @ Nullable
588
597
public <T > T body (Class <T > bodyType ) {
589
598
return readBody (bodyType , bodyType );
590
599
}
591
600
592
601
@ Override
602
+ @ Nullable
593
603
public <T > T body (ParameterizedTypeReference <T > bodyType ) {
594
604
Type type = bodyType .getType ();
595
605
Class <T > bodyClass = bodyClass (type );
@@ -637,6 +647,7 @@ public ResponseEntity<Void> toBodilessEntity() {
637
647
}
638
648
639
649
650
+ @ Nullable
640
651
private <T > T readBody (Type bodyType , Class <T > bodyClass ) {
641
652
return DefaultRestClient .this .readWithMessageConverters (this .clientResponse , this ::applyStatusHandlers ,
642
653
bodyType , bodyClass );
0 commit comments