Skip to content

Commit 530c7be

Browse files
committed
Do not auto-configure HttpMessageConverters in reactive web apps
Closes gh-15712
1 parent 6abd18a commit 530c7be

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.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.gson.GsonAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration.NotReactiveWebApplicationCondition;
2832
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
2933
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
3034
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.context.annotation.Import;
3439
import org.springframework.http.converter.HttpMessageConverter;
@@ -49,6 +54,7 @@
4954
*/
5055
@Configuration
5156
@ConditionalOnClass(HttpMessageConverter.class)
57+
@Conditional(NotReactiveWebApplicationCondition.class)
5258
@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class,
5359
JsonbAutoConfiguration.class })
5460
@Import({ JacksonHttpMessageConvertersConfiguration.class,
@@ -93,4 +99,17 @@ public StringHttpMessageConverter stringHttpMessageConverter() {
9399

94100
}
95101

102+
static class NotReactiveWebApplicationCondition extends NoneNestedConditions {
103+
104+
NotReactiveWebApplicationCondition() {
105+
super(ConfigurationPhase.PARSE_CONFIGURATION);
106+
}
107+
108+
@ConditionalOnWebApplication(type = Type.REACTIVE)
109+
private static class ReactiveWebApplication {
110+
111+
}
112+
113+
}
114+
96115
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationTests.java

Lines changed: 21 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.
@@ -32,6 +32,8 @@
3232
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3333
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3434
import org.springframework.boot.test.context.runner.ContextConsumer;
35+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
36+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3537
import org.springframework.context.annotation.Bean;
3638
import org.springframework.context.annotation.Configuration;
3739
import org.springframework.context.support.GenericApplicationContext;
@@ -257,6 +259,24 @@ public void jsonbIsPreferredIfJacksonAndGsonAreNotAvailable() {
257259
"jsonbHttpMessageConverter"));
258260
}
259261

262+
@Test
263+
public void whenServletWebApplicationHttpMessageConvertersIsConfigured() {
264+
new WebApplicationContextRunner()
265+
.withConfiguration(AutoConfigurations
266+
.of(HttpMessageConvertersAutoConfiguration.class))
267+
.run((context) -> assertThat(context)
268+
.hasSingleBean(HttpMessageConverters.class));
269+
}
270+
271+
@Test
272+
public void whenReactiveWebApplicationHttpMessageConvertersRestTemplateIsNotConfigured() {
273+
new ReactiveWebApplicationContextRunner()
274+
.withConfiguration(AutoConfigurations
275+
.of(HttpMessageConvertersAutoConfiguration.class))
276+
.run((context) -> assertThat(context)
277+
.doesNotHaveBean(HttpMessageConverters.class));
278+
}
279+
260280
private ApplicationContextRunner allOptionsRunner() {
261281
return this.contextRunner
262282
.withConfiguration(AutoConfigurations.of(GsonAutoConfiguration.class,

0 commit comments

Comments
 (0)