Browse Source

RestTemplateCustomizers should be applied at the end

See gh-13358
pull/13214/merge
Dmytro Nosan 8 years ago committed by Stephane Nicoll
parent
commit
ce3420748f
  1. 3
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java
  2. 33
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java

3
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java

@ -531,12 +531,13 @@ public class RestTemplateBuilder { @@ -531,12 +531,13 @@ public class RestTemplateBuilder {
if (this.basicAuthorization != null) {
restTemplate.getInterceptors().add(this.basicAuthorization);
}
restTemplate.getInterceptors().addAll(this.interceptors);
if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) {
for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) {
customizer.customize(restTemplate);
}
}
restTemplate.getInterceptors().addAll(this.interceptors);
return restTemplate;
}

33
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java

@ -32,6 +32,7 @@ import org.springframework.http.client.BufferingClientHttpRequestFactory; @@ -32,6 +32,7 @@ import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
@ -497,6 +498,38 @@ public class RestTemplateBuilderTests { @@ -497,6 +498,38 @@ public class RestTemplateBuilderTests {
.isInstanceOf(BufferingClientHttpRequestFactory.class);
}
@Test
public void customizerShouldBeAppliedInTheEnd() {
ClientHttpRequestInterceptor interceptor = this.interceptor;
HttpMessageConverter<Object> messageConverter = this.messageConverter;
ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
this.builder.interceptors(interceptor).messageConverters(messageConverter)
.rootUri("http://localhost:8080").errorHandler(errorHandler)
.basicAuthorization("spring", "boot")
.requestFactory(HttpComponentsClientHttpRequestFactory.class)
.customizers((restTemplate) -> {
ClientHttpRequestFactory requestFactory = restTemplate
.getRequestFactory();
assertThat(restTemplate.getInterceptors()).hasSize(2)
.contains(interceptor).anyMatch(
(ic) -> ic instanceof BasicAuthorizationInterceptor);
assertThat(restTemplate.getMessageConverters())
.contains(messageConverter);
assertThat(restTemplate.getUriTemplateHandler())
.isInstanceOf(RootUriTemplateHandler.class);
assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
assertThat(requestFactory)
.isInstanceOf(InterceptingClientHttpRequestFactory.class);
assertThat(ReflectionTestUtils.getField(requestFactory,
"requestFactory")).isInstanceOf(
HttpComponentsClientHttpRequestFactory.class);
}).build();
}
public static class RestTemplateSubclass extends RestTemplate {
}

Loading…
Cancel
Save