From ce3420748f41bb7e4288e38be592a5cacfd5848a Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Mon, 4 Jun 2018 18:16:08 +0300 Subject: [PATCH 1/2] 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 { } From c612ed26950396789ae85d1ee1ce2a509f8b74ea Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 5 Jun 2018 09:45:19 +0200 Subject: [PATCH 2/2] Polish "RestTemplateCustomizers should be applied at the end" Closes gh-13358 --- .../boot/web/client/RestTemplateBuilder.java | 1 - .../web/client/RestTemplateBuilderTests.java | 58 +++++++++---------- 2 files changed, 26 insertions(+), 33 deletions(-) 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 4cdf7bcdcf..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 @@ -532,7 +532,6 @@ public class RestTemplateBuilder { restTemplate.getInterceptors().add(this.basicAuthorization); } restTemplate.getInterceptors().addAll(this.interceptors); - if (!CollectionUtils.isEmpty(this.restTemplateCustomizers)) { for (RestTemplateCustomizer customizer : this.restTemplateCustomizers) { customizer.customize(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 58c7a44591..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 @@ -389,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(); @@ -498,38 +524,6 @@ 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 { }