From 882e29dc7fd870d0990b31cb02d591329a5efb86 Mon Sep 17 00:00:00 2001 From: saravanakumar Ramasamy Date: Sat, 7 Oct 2023 19:28:20 +0530 Subject: [PATCH 1/2] Remove @ConditionalOnMissingBean from RestTemplateBuilderConfigurer See gh-37746 --- .../client/RestTemplateAutoConfiguration.java | 1 - .../RestTemplateAutoConfigurationTests.java | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java index ce89524c27..70bc8c50c6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java @@ -45,7 +45,6 @@ public class RestTemplateAutoConfiguration { @Bean @Lazy - @ConditionalOnMissingBean public RestTemplateBuilderConfigurer restTemplateBuilderConfigurer( ObjectProvider messageConverters, ObjectProvider restTemplateCustomizers, diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java index a80f93a737..5365bd047d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java @@ -21,6 +21,7 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.support.BeanDefinitionOverrideException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; @@ -42,6 +43,7 @@ import org.springframework.mock.http.client.MockClientHttpResponse; import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -65,6 +67,15 @@ class RestTemplateAutoConfigurationTests { .isTrue()); } + @Test + void assertExceptionWhenCustomRestTemplateConfigurerIsDefined() { + this.contextRunner + .withUserConfiguration(RestTemplateCustomConfigurerConfig.class) + .run((context) -> assertThrows( BeanDefinitionOverrideException.class, () ->{ + context.getBeanFactory().getBeanDefinition("restTemplateBuilderConfigurer"); + })); + } + @Test void restTemplateBuilderShouldBeLazilyDefined() { this.contextRunner @@ -263,6 +274,16 @@ class RestTemplateAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class RestTemplateCustomConfigurerConfig { + + @Bean + RestTemplateBuilderConfigurer restTemplateBuilderConfigurer() { + return new RestTemplateBuilderConfigurer(); + } + + } + static class CustomHttpMessageConverter extends StringHttpMessageConverter { } From 6874a2fb9a01ff1f70b562138f1c8325f4e70d83 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 11 Oct 2023 14:03:12 +0200 Subject: [PATCH 2/2] Polish "Remove @ConditionalOnMissingBean from RestTemplateBuilderConfigurer" See gh-37746 --- .../client/RestTemplateAutoConfigurationTests.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java index 5365bd047d..8e576929f2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java @@ -43,7 +43,6 @@ import org.springframework.mock.http.client.MockClientHttpResponse; import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -68,12 +67,11 @@ class RestTemplateAutoConfigurationTests { } @Test - void assertExceptionWhenCustomRestTemplateConfigurerIsDefined() { - this.contextRunner - .withUserConfiguration(RestTemplateCustomConfigurerConfig.class) - .run((context) -> assertThrows( BeanDefinitionOverrideException.class, () ->{ - context.getBeanFactory().getBeanDefinition("restTemplateBuilderConfigurer"); - })); + void shouldFailOnCustomRestTemplateBuilderConfigurer() { + this.contextRunner.withUserConfiguration(RestTemplateCustomConfigurerConfig.class) + .run((context) -> assertThat(context).getFailure() + .isInstanceOf(BeanDefinitionOverrideException.class) + .hasMessageContaining("with name 'restTemplateBuilderConfigurer'")); } @Test