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
This commit is contained in:
Andy Wilkinson
2016-08-18 11:40:23 +01:00
parent a2420bacfb
commit 70b5c97c7f
2 changed files with 14 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -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 {
}