From 70b5c97c7fcdc21f4057df82c97977354690d955 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 18 Aug 2016 11:40:23 +0100 Subject: [PATCH] Set original, possibly wrapped request factory on built RestTemplate Previously, RestTemplateBuilder unwrapped the request factory to allow the actual factory to be customised but then set this unwrapped factory on the template that is being built. This meant that any wrappers were lost. This commit updates the build to unwrap the factory prior to it being customised, but to the set the original, possibly wrapped factory on the template that is being built. Closes gh-6685 --- .../boot/web/client/RestTemplateBuilder.java | 6 ++++-- .../boot/web/client/RestTemplateBuilderTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java index c7b7d4b84d..0b757dbc4c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java @@ -463,14 +463,16 @@ public class RestTemplateBuilder { private void configureRequestFactory(RestTemplate restTemplate) { ClientHttpRequestFactory requestFactory = null; if (this.requestFactory != null) { - requestFactory = unwrapRequestFactoryIfNecessary(this.requestFactory); + requestFactory = this.requestFactory; } else if (this.detectRequestFactory) { requestFactory = detectRequestFactory(); } if (requestFactory != null) { + ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary( + requestFactory); for (RequestFactoryCustomizer customizer : this.requestFactoryCustomizers) { - customizer.customize(requestFactory); + customizer.customize(unwrappedRequestFactory); } restTemplate.setRequestFactory(requestFactory); } diff --git a/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java index bdc81116b0..c7e7d51736 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java @@ -474,6 +474,16 @@ public class RestTemplateBuilderTests { .isEqualTo(1234); } + @Test + public void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() { + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + RestTemplate template = this.builder + .requestFactory(new BufferingClientHttpRequestFactory(requestFactory)) + .build(); + assertThat(template.getRequestFactory()) + .isInstanceOf(BufferingClientHttpRequestFactory.class); + } + public static class RestTemplateSubclass extends RestTemplate { }