mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Fix welcome page support in WebFlux
Prior to this commit, the welcome page support implemented in gh-9785 would override existing index views in both annotation and functional variants. This comes from the fact that the feature was implemented as a `RouterFunction` configured in the main `RouterFunctionMapping` bean. Due to ordering issues between mappings, this would override existing application mappings in some cases. This commit ensures that the welcome page `RouterFunction` is contributed to the context in its own handler mapping, ordered after the application ones. Fixes gh-21909
This commit is contained in:
parent
7500f0f22d
commit
af2666df79
@ -63,6 +63,7 @@ import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
|
||||
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import org.springframework.web.reactive.function.server.support.RouterFunctionMapping;
|
||||
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
@ -101,12 +102,18 @@ public class WebFluxAutoConfiguration {
|
||||
public static class WelcomePageConfiguration {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> welcomePageRouterFunction(ApplicationContext applicationContext,
|
||||
public RouterFunctionMapping welcomePageRouterFunctionMapping(ApplicationContext applicationContext,
|
||||
WebFluxProperties webFluxProperties, ResourceProperties resourceProperties) {
|
||||
WelcomePageRouterFunctionFactory factory = new WelcomePageRouterFunctionFactory(
|
||||
new TemplateAvailabilityProviders(applicationContext), applicationContext,
|
||||
resourceProperties.getStaticLocations(), webFluxProperties.getStaticPathPattern());
|
||||
return factory.createRouterFunction();
|
||||
RouterFunction<ServerResponse> routerFunction = factory.createRouterFunction();
|
||||
if (routerFunction != null) {
|
||||
RouterFunctionMapping routerFunctionMapping = new RouterFunctionMapping(routerFunction);
|
||||
routerFunctionMapping.setOrder(1);
|
||||
return routerFunctionMapping;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -62,6 +62,7 @@ import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
|
||||
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
||||
import org.springframework.web.reactive.function.server.support.RouterFunctionMapping;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.resource.CachingResourceResolver;
|
||||
import org.springframework.web.reactive.resource.CachingResourceTransformer;
|
||||
@ -108,6 +109,7 @@ class WebFluxAutoConfigurationTests {
|
||||
assertThat(context).getBeans(RequestMappingHandlerMapping.class).hasSize(1);
|
||||
assertThat(context).getBeans(RequestMappingHandlerAdapter.class).hasSize(1);
|
||||
assertThat(context).getBeans(RequestedContentTypeResolver.class).hasSize(1);
|
||||
assertThat(context).getBeans(RouterFunctionMapping.class).hasSize(1);
|
||||
assertThat(context.getBean("resourceHandlerMapping", HandlerMapping.class)).isNotNull();
|
||||
});
|
||||
}
|
||||
@ -440,6 +442,15 @@ class WebFluxAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void welcomePageHandlerMapping() {
|
||||
this.contextRunner.withPropertyValues("spring.resources.static-locations=classpath:/welcome-page/").run((context) -> {
|
||||
assertThat(context).getBeans(RouterFunctionMapping.class).hasSize(2);
|
||||
assertThat(context.getBean("welcomePageRouterFunctionMapping", HandlerMapping.class)).isNotNull()
|
||||
.extracting("order").isEqualTo(1);
|
||||
});
|
||||
}
|
||||
|
||||
private Map<PathPattern, Object> getHandlerMap(ApplicationContext context) {
|
||||
HandlerMapping mapping = context.getBean("resourceHandlerMapping", HandlerMapping.class);
|
||||
if (mapping instanceof SimpleUrlHandlerMapping) {
|
||||
|
Loading…
Reference in New Issue
Block a user