diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java index c47afea1633..ad976e3b2fd 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java @@ -31,34 +31,42 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.http.HttpStatus; -import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.lang.Nullable; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebHandler; -import org.springframework.web.server.adapter.HttpWebHandlerAdapter; -import org.springframework.web.server.adapter.WebHttpHandlerBuilder; /** - * Central dispatcher for HTTP request handlers/controllers. Dispatches to registered - * handlers for processing a web request, providing convenient mapping facilities. + * Central dispatcher for HTTP request handlers/controllers. Dispatches to + * registered handlers for processing a request, providing convenient mapping + * facilities. * - *
It can use any {@link HandlerMapping} implementation to control the routing of - * requests to handler objects. HandlerMapping objects can be defined as beans in - * the application context. + *
{@code DispatcherHandler} discovers the delegate components it needs from + * Spring configuration. It detects the following in the application context: + *
It can use any {@link HandlerAdapter}; this allows for using any handler interface. - * HandlerAdapter objects can be added as beans in the application context. + *
{@code DispatcherHandler} s also designed to be a Spring bean itself and + * implements {@link ApplicationContextAware} for access to the context it runs + * in. If {@code DispatcherHandler} is declared with the bean name "webHandler" + * it is discovered by {@link WebHttpHandlerBuilder#applicationContext} which + * creates a processing chain together with {@code WebFilter}, + * {@code WebExceptionHandler} and others. * - *
It can use any {@link HandlerResultHandler}; this allows to process the result of - * the request handling. HandlerResultHandler objects can be added as beans in the - * application context. + *
A {@code DispatcherHandler} bean declaration is included in + * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux} + * configuration. * * @author Rossen Stoyanchev * @author Sebastien Deleuze * @author Juergen Hoeller * @since 5.0 + * @see WebHttpHandlerBuilder#applicationContext(ApplicationContext) */ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -181,39 +189,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue()); } - - /** - * Expose a dispatcher-based {@link WebHandler} for the given application context, - * typically for further configuration with filters and exception handlers through - * a {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder}. - * @param applicationContext the application context to find the handler beans in - * @see #DispatcherHandler(ApplicationContext) - * @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#webHandler - */ - public static WebHandler toWebHandler(ApplicationContext applicationContext) { - return new DispatcherHandler(applicationContext); - } - - /** - * Expose a dispatcher-based {@link HttpHandler} for the given application context, - * typically for direct registration with an engine adapter such as - * {@link org.springframework.http.server.reactive.ServletHttpHandlerAdapter}. - * - *
Delegates to {@link WebHttpHandlerBuilder#applicationContext} that - * detects the target {@link DispatcherHandler} along with - * {@link org.springframework.web.server.WebFilter}s, and - * {@link org.springframework.web.server.WebExceptionHandler}s in the given - * ApplicationContext. - * - * @param context the application context to find the handler beans in - * @see #DispatcherHandler(ApplicationContext) - * @see HttpWebHandlerAdapter - * @see org.springframework.http.server.reactive.ServletHttpHandlerAdapter - * @see org.springframework.http.server.reactive.ReactorHttpHandlerAdapter - * @see org.springframework.http.server.reactive.UndertowHttpHandlerAdapter - */ - public static HttpHandler toHttpHandler(ApplicationContext context) { - return WebHttpHandlerBuilder.applicationContext(context).build(); - } - } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java index 50349f48cf6..532a30b5210 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java @@ -28,10 +28,10 @@ import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; -import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.config.EnableWebFlux; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * Integration tests that demonstrate running multiple applications under @@ -55,8 +55,8 @@ public class ContextPathIntegrationTests { context2.register(WebApp2Config.class); context2.refresh(); - HttpHandler webApp1Handler = DispatcherHandler.toHttpHandler(context1); - HttpHandler webApp2Handler = DispatcherHandler.toHttpHandler(context2); + HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build(); + HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build(); this.server = new ReactorHttpServer(); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java index 10fd9baa668..3afe7d6def2 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java @@ -61,6 +61,7 @@ import org.springframework.web.reactive.socket.server.upgrade.ReactorNettyReques import org.springframework.web.reactive.socket.server.upgrade.RxNettyRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import static org.junit.Assume.*; @@ -160,7 +161,7 @@ public abstract class AbstractWebSocketIntegrationTests { context.register(DispatcherConfig.class, this.serverConfigClass); context.register(getWebConfigClass()); context.refresh(); - return DispatcherHandler.toHttpHandler(context); + return WebHttpHandlerBuilder.applicationContext(context).build(); } protected URI getUrl(String path) throws URISyntaxException {