/** * Convert the given {@linkplain RouterFunction router function} into a {@link WebHandler}. * This conversion uses {@linkplain HandlerStrategies#builder() default strategies}. * @param routerFunction the router function to convert * @return a web handler that handles web request using the given router function */ public static WebHandler toWebHandler(RouterFunction<?> routerFunction) { return toWebHandler(routerFunction, HandlerStrategies.withDefaults()); }
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(this.handlerStrategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) .localeContextResolver(this.handlerStrategies.localeContextResolver()); }
/** * Convert the given {@linkplain RouterFunction router function} into a {@link HttpHandler}, * using the given strategies. * <p>The returned {@code HttpHandler} can be adapted to run in * <ul> * <li>Servlet 3.1+ using the * {@link org.springframework.http.server.reactive.ServletHttpHandlerAdapter},</li> * <li>Reactor using the * {@link org.springframework.http.server.reactive.ReactorHttpHandlerAdapter},</li> * <li>Undertow using the * {@link org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.</li> * </ul> * @param routerFunction the router function to convert * @param strategies the strategies to use * @return an http handler that handles HTTP request using the given router function */ public static HttpHandler toHttpHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) { WebHandler webHandler = toWebHandler(routerFunction, strategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(strategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(strategies.exceptionHandlers())) .localeContextResolver(strategies.localeContextResolver()) .build(); }
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(this.handlerStrategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) .localeContextResolver(this.handlerStrategies.localeContextResolver()); }
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(this.handlerStrategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) .localeContextResolver(this.handlerStrategies.localeContextResolver()); }
public static void main(String[] args) throws Exception { long t0 = System.currentTimeMillis(); GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(RouterFunction.class, () -> RouterFunctions.route(GET("/"), request -> ok().body(Mono.just("Hello"), String.class))); context.registerBean("webHandler", WebHandler.class, () -> RouterFunctions .toWebHandler(context.getBean(RouterFunction.class))); context.refresh(); ApplicationBuilder.start(context, b -> { System.err.println("Started HttpServer: " + (System.currentTimeMillis() - t0) + "ms"); }); } }
public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); PostRepository posts = new PostRepository(); PostHandler postHandler = new PostHandler(posts); Routes routesBean = new Routes(postHandler); context.registerBean(PostRepository.class, () -> posts); context.registerBean(PostHandler.class, () -> postHandler); context.registerBean(Routes.class, () -> routesBean); context.registerBean(WebHandler.class, () -> RouterFunctions.toWebHandler(routesBean.routes(), HandlerStrategies.builder().build())); context.refresh(); nettyServer(context).onDispose().block(); }