Skip to content

Commit 6abd18a

Browse files
committed
Do not auto-configure RestTemplateBuilder in reactive web apps
Closes gh-15718
1 parent 5d60d6b commit 6abd18a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,16 @@
2424
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29+
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
2730
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
2831
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration.NotReactiveWebApplicationCondition;
2933
import org.springframework.boot.web.client.RestTemplateBuilder;
3034
import org.springframework.boot.web.client.RestTemplateCustomizer;
3135
import org.springframework.context.annotation.Bean;
36+
import org.springframework.context.annotation.Conditional;
3237
import org.springframework.context.annotation.Configuration;
3338
import org.springframework.util.CollectionUtils;
3439
import org.springframework.web.client.RestTemplate;
@@ -43,6 +48,7 @@
4348
@Configuration
4449
@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class)
4550
@ConditionalOnClass(RestTemplate.class)
51+
@Conditional(NotReactiveWebApplicationCondition.class)
4652
public class RestTemplateAutoConfiguration {
4753

4854
private final ObjectProvider<HttpMessageConverters> messageConverters;
@@ -73,4 +79,17 @@ public RestTemplateBuilder restTemplateBuilder() {
7379
return builder;
7480
}
7581

82+
static class NotReactiveWebApplicationCondition extends NoneNestedConditions {
83+
84+
NotReactiveWebApplicationCondition() {
85+
super(ConfigurationPhase.PARSE_CONFIGURATION);
86+
}
87+
88+
@ConditionalOnWebApplication(type = Type.REACTIVE)
89+
private static class ReactiveWebApplication {
90+
91+
}
92+
93+
}
94+
7695
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfigurationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
2525
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
2626
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
28+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2729
import org.springframework.boot.web.client.RestTemplateBuilder;
2830
import org.springframework.boot.web.client.RestTemplateCustomizer;
2931
import org.springframework.context.annotation.Bean;
@@ -124,6 +126,24 @@ public void builderShouldBeFreshForEachUse() {
124126
.run((context) -> assertThat(context).hasNotFailed());
125127
}
126128

129+
@Test
130+
public void whenServletWebApplicationRestTemplateBuilderIsConfigured() {
131+
new WebApplicationContextRunner()
132+
.withConfiguration(
133+
AutoConfigurations.of(RestTemplateAutoConfiguration.class))
134+
.run((context) -> assertThat(context)
135+
.hasSingleBean(RestTemplateBuilder.class));
136+
}
137+
138+
@Test
139+
public void whenReactiveWebApplicationRestTemplateIsNotConfigured() {
140+
new ReactiveWebApplicationContextRunner()
141+
.withConfiguration(
142+
AutoConfigurations.of(RestTemplateAutoConfiguration.class))
143+
.run((context) -> assertThat(context)
144+
.doesNotHaveBean(RestTemplateBuilder.class));
145+
}
146+
127147
@Configuration
128148
static class RestTemplateConfig {
129149

0 commit comments

Comments
 (0)