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..ad1efb7159 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,12 @@ 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..cd87b9a861 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; @@ -388,6 +389,32 @@ public class RestTemplateBuilderTests { verify(customizer2).customize(template); } + @Test + public void customizerShouldBeAppliedInTheEnd() { + ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class); + ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + this.builder.interceptors(this.interceptor) + .messageConverters(this.messageConverter).rootUri("http://localhost:8080") + .errorHandler(errorHandler).basicAuthorization("spring", "boot") + .requestFactory(() -> requestFactory).customizers((restTemplate) -> { + assertThat(restTemplate.getInterceptors()).hasSize(2) + .contains(this.interceptor).anyMatch( + (ic) -> ic instanceof BasicAuthorizationInterceptor); + assertThat(restTemplate.getMessageConverters()) + .contains(this.messageConverter); + assertThat(restTemplate.getUriTemplateHandler()) + .isInstanceOf(RootUriTemplateHandler.class); + assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler); + ClientHttpRequestFactory actualRequestFactory = restTemplate + .getRequestFactory(); + assertThat(actualRequestFactory) + .isInstanceOf(InterceptingClientHttpRequestFactory.class); + assertThat(ReflectionTestUtils.getField(actualRequestFactory, + "requestFactory")).isSameAs(requestFactory); + }).build(); + + } + @Test public void buildShouldReturnRestTemplate() { RestTemplate template = this.builder.build();