Fix startup failure when WebFlux auto-config is skipped

Prior to this commit, the `HttpHandlerAutoConfiguration` would rely on
the `WebFluxProperties` bean being present - this is most of the time
true when the appplication is using the WebFlux auto-configuration.

If the application is overriding the WebFlux auto-configuration and
providing its own setup, the properties bean is not present and we
should skip its usage.

Fixes gh-20891
This commit is contained in:
Brian Clozel
2020-04-10 14:16:16 +02:00
parent b6076f5b10
commit 0611b6d3eb
2 changed files with 22 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive;
import java.util.Collections;
import java.util.Map;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -60,9 +61,10 @@ public class HttpHandlerAutoConfiguration {
}
@Bean
public HttpHandler httpHandler(WebFluxProperties properties) {
public HttpHandler httpHandler(ObjectProvider<WebFluxProperties> propsProvider) {
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();
if (StringUtils.hasText(properties.getBasePath())) {
WebFluxProperties properties = propsProvider.getIfAvailable();
if (properties != null && StringUtils.hasText(properties.getBasePath())) {
Map<String, HttpHandler> handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);
return new ContextPathCompositeHandler(handlersMap);
}

View File

@@ -25,8 +25,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.WebHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@@ -58,6 +60,12 @@ class HttpHandlerAutoConfigurationTests {
.run((context) -> assertThat(context).hasSingleBean(HttpHandler.class));
}
@Test
void shouldConfigureHttpHandlerWithoutWebFluxAutoConfiguration() {
this.contextRunner.withUserConfiguration(CustomWebHandler.class)
.run((context) -> assertThat(context).hasSingleBean(HttpHandler.class));
}
@Test
void shouldConfigureBasePathCompositeHandler() {
this.contextRunner.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
@@ -85,4 +93,14 @@ class HttpHandlerAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class CustomWebHandler {
@Bean
WebHandler webHandler() {
return new DispatcherHandler();
}
}
}