From ce3420748f41bb7e4288e38be592a5cacfd5848a Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Mon, 4 Jun 2018 18:16:08 +0300 Subject: [PATCH] RestTemplateCustomizers should be applied at the end See gh-13358 --- .../boot/web/client/RestTemplateBuilder.java | 3 +- .../web/client/RestTemplateBuilderTests.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java index 88cf18909c..4cdf7bcdcf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java @@ -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; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java index d94e3f9557..58c7a44591 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/RestTemplateBuilderTests.java +++ b/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; 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 { .isInstanceOf(BufferingClientHttpRequestFactory.class); } + @Test + public void customizerShouldBeAppliedInTheEnd() { + + ClientHttpRequestInterceptor interceptor = this.interceptor; + HttpMessageConverter 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 { }