Commit 70b5c97c authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent a2420bac
...@@ -463,14 +463,16 @@ public class RestTemplateBuilder { ...@@ -463,14 +463,16 @@ public class RestTemplateBuilder {
private void configureRequestFactory(RestTemplate restTemplate) { private void configureRequestFactory(RestTemplate restTemplate) {
ClientHttpRequestFactory requestFactory = null; ClientHttpRequestFactory requestFactory = null;
if (this.requestFactory != null) { if (this.requestFactory != null) {
requestFactory = unwrapRequestFactoryIfNecessary(this.requestFactory); requestFactory = this.requestFactory;
} }
else if (this.detectRequestFactory) { else if (this.detectRequestFactory) {
requestFactory = detectRequestFactory(); requestFactory = detectRequestFactory();
} }
if (requestFactory != null) { if (requestFactory != null) {
ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary(
requestFactory);
for (RequestFactoryCustomizer customizer : this.requestFactoryCustomizers) { for (RequestFactoryCustomizer customizer : this.requestFactoryCustomizers) {
customizer.customize(requestFactory); customizer.customize(unwrappedRequestFactory);
} }
restTemplate.setRequestFactory(requestFactory); restTemplate.setRequestFactory(requestFactory);
} }
......
...@@ -474,6 +474,16 @@ public class RestTemplateBuilderTests { ...@@ -474,6 +474,16 @@ public class RestTemplateBuilderTests {
.isEqualTo(1234); .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 { public static class RestTemplateSubclass extends RestTemplate {
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment