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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user