Java Code Examples for org.springframework.web.reactive.function.server.RouterFunctions#toHttpHandler()

The following examples show how to use org.springframework.web.reactive.function.server.RouterFunctions#toHttpHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandaloneApplication.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 6 votes vote down vote up
public static void main(String... args) {
    long start = System.currentTimeMillis();
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(routes(
        new BCryptPasswordEncoder(18)
    ));
    ReactorHttpHandlerAdapter reactorHttpHandler = new ReactorHttpHandlerAdapter(httpHandler);

    DisposableServer server = HttpServer.create()
                                        .host("localhost")
                                        .port(8080)
                                        .handle(reactorHttpHandler)
                                        .bindNow();

    LOGGER.debug("Started in " + (System.currentTimeMillis() - start) + " ms");

    server.onDispose()
          .block();
}
 
Example 2
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 3
Source File: Spring5ReactiveServerClientIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeAll
public static void setUp() throws Exception {
    HttpServer server = HttpServer.create()
        .host("localhost")
        .port(8080);
    RouterFunction<?> route = RouterFunctions.route(POST("/task/process"), request -> ServerResponse.ok()
        .body(request.bodyToFlux(Task.class)
            .map(ll -> new Task("TaskName", 1)), Task.class))
        .and(RouterFunctions.route(GET("/task"), request -> ServerResponse.ok()
            .body(Mono.just("server is alive"), String.class)));
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
    ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
    disposableServer = server.handle(adapter)
        .bindNow();
}
 
Example 4
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 5
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 6
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 7
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 8
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 9
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 10
Source File: HttpServerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void start() throws Exception {
	HttpHandler httpHandler = RouterFunctions.toHttpHandler(
			route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")));

	this.server = new ReactorHttpServer();
	this.server.setHandler(httpHandler);
	this.server.afterPropertiesSet();
	this.server.start();

	this.client = WebTestClient.bindToServer()
			.baseUrl("http://localhost:" + this.server.getPort())
			.build();
}
 
Example 11
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 12
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 13
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 14
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 15
Source File: HttpServerConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);

     ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
     registrationBean.setLoadOnStartup(1);
     registrationBean.setAsyncSupported(true);
     
 	System.out.println("starts server");		
     return registrationBean;
 }
 
Example 16
Source File: ExportManageServerRunner.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * 暴露6200管理端口
 */
@Override
public void run(ApplicationArguments args) throws Exception {
    RouterFunction manageRouterFunction = RouterFunctions
            .route(RequestPredicates.GET("/ok"), req -> {
                return ServerResponse.ok()
                        .syncBody("online");
            })
            .andRoute(RequestPredicates.GET("/cmd/{line}"), request -> {
                String pathVar = request.pathVariable("line");
                try {
                    if ("down".equals(pathVar)) {
                        refuseRequestService.setRefuseRequest(true);
                    } else if ("up".equals(pathVar)) {
                        refuseRequestService.setRefuseRequest(false);
                    }
                    return ServerResponse.ok().body(Mono.just("ok"), String.class);
                } catch (Throwable e) {
                    LOGGER.error("{} request err!", pathVar, e);
                    return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
                }
            });

    HttpHandler handler = RouterFunctions.toHttpHandler(manageRouterFunction);
    int managePort = serverPort - 1000;

    ReactorHttpHandlerAdapter inboundAdapter = new ReactorHttpHandlerAdapter(handler);

    // manage port
    HttpServer.create().port(managePort).handle(inboundAdapter).bind();
    // HttpServer.create(managePort).newHandler(inboundAdapter).block();

    LOGGER.info("management services run on port:{}", managePort);
}
 
Example 17
Source File: HttpServerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void start() throws Exception {
	HttpHandler httpHandler = RouterFunctions.toHttpHandler(
			route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")));

	this.server = new ReactorHttpServer();
	this.server.setHandler(httpHandler);
	this.server.afterPropertiesSet();
	this.server.start();

	this.client = WebTestClient.bindToServer()
			.baseUrl("http://localhost:" + this.server.getPort())
			.build();
}
 
Example 18
Source File: FunctionEndpointInitializer.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
private HttpWebHandlerAdapter httpHandler(GenericApplicationContext context) {
	return (HttpWebHandlerAdapter) RouterFunctions.toHttpHandler(context.getBean(RouterFunction.class),
			HandlerStrategies.empty().exceptionHandler(context.getBean(WebExceptionHandler.class))
					.codecs(config -> config.registerDefaults(true)).build());
}