From 6abd18ae9630577864a2a130d7d903657bbc1fb7 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 18 Jan 2019 14:34:35 -0500 Subject: [PATCH] Do not auto-configure RestTemplateBuilder in reactive web apps Closes gh-15718 --- .../client/RestTemplateAutoConfiguration.java | 21 ++++++++++++++++++- .../RestTemplateAutoConfigurationTests.java | 20 ++++++++++++++++++ 2 files changed, 40 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 620f044e0a..e9578fea58 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,11 +24,16 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; +import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration.NotReactiveWebApplicationCondition; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateCustomizer; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.util.CollectionUtils; import org.springframework.web.client.RestTemplate; @@ -43,6 +48,7 @@ import org.springframework.web.client.RestTemplate; @Configuration @AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class) @ConditionalOnClass(RestTemplate.class) +@Conditional(NotReactiveWebApplicationCondition.class) public class RestTemplateAutoConfiguration { private final ObjectProvider messageConverters; @@ -73,4 +79,17 @@ public class RestTemplateAutoConfiguration { return builder; } + static class NotReactiveWebApplicationCondition extends NoneNestedConditions { + + NotReactiveWebApplicationCondition() { + super(ConfigurationPhase.PARSE_CONFIGURATION); + } + + @ConditionalOnWebApplication(type = Type.REACTIVE) + private static class ReactiveWebApplication { + + } + + } + } 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 6e54be2534..bd499fc9d7 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 @@ -24,6 +24,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateCustomizer; import org.springframework.context.annotation.Bean; @@ -124,6 +126,24 @@ public class RestTemplateAutoConfigurationTests { .run((context) -> assertThat(context).hasNotFailed()); } + @Test + public void whenServletWebApplicationRestTemplateBuilderIsConfigured() { + new WebApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(RestTemplateAutoConfiguration.class)) + .run((context) -> assertThat(context) + .hasSingleBean(RestTemplateBuilder.class)); + } + + @Test + public void whenReactiveWebApplicationRestTemplateIsNotConfigured() { + new ReactiveWebApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(RestTemplateAutoConfiguration.class)) + .run((context) -> assertThat(context) + .doesNotHaveBean(RestTemplateBuilder.class)); + } + @Configuration static class RestTemplateConfig {