Do not auto-configure RestTemplateBuilder in reactive web apps

Closes gh-15718
This commit is contained in:
Andy Wilkinson
2019-01-18 14:34:35 -05:00
parent 5d60d6bd62
commit 6abd18ae96
2 changed files with 40 additions and 1 deletions

View File

@@ -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<HttpMessageConverters> 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 {
}
}
}

View File

@@ -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 {