diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java index c9e4681a92f..0e89d866928 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java @@ -196,7 +196,7 @@ final class DefaultRestClient implements RestClient { MediaType contentType = getContentType(clientResponse); - try (clientResponse) { + try { callback.run(); IntrospectingClientHttpResponse responseWrapper = new IntrospectingClientHttpResponse(clientResponse); @@ -240,17 +240,18 @@ final class DefaultRestClient implements RestClient { ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause); if (observation != null) { observation.error(restClientException); - observation.stop(); } throw restClientException; } catch (RestClientException restClientException) { if (observation != null) { observation.error(restClientException); - observation.stop(); } throw restClientException; } + finally { + clientResponse.close(); + } } private static MediaType getContentType(ClientHttpResponse clientResponse) {