use of org.springframework.web.reactive.function.server.RouterFunction in project tutorials by eugenp.
the class Spring5ReactiveServerClientIntegrationTest method setUp.
@BeforeAll
public static void setUp() throws Exception {
HttpServer server = HttpServer.create("localhost", 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);
nettyContext = server.newHandler(adapter).block();
}
use of org.springframework.web.reactive.function.server.RouterFunction in project tutorials by eugenp.
the class RootServlet method routingFunction.
private static RouterFunction<?> routingFunction() {
return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))).andRoute(POST("/login"), serverRequest -> serverRequest.body(toFormData()).map(MultiValueMap::toSingleValueMap).map(formData -> {
System.out.println("form data: " + formData.toString());
if ("baeldung".equals(formData.get("user")) && "you_know_what_to_do".equals(formData.get("token"))) {
return ok().body(Mono.just("welcome back!"), String.class).block();
}
return ServerResponse.badRequest().build().block();
})).andRoute(POST("/upload"), serverRequest -> serverRequest.body(toDataBuffers()).collectList().map(dataBuffers -> {
AtomicLong atomicLong = new AtomicLong(0);
dataBuffers.forEach(d -> atomicLong.addAndGet(d.asByteBuffer().array().length));
System.out.println("data length:" + atomicLong.get());
return ok().body(fromObject(atomicLong.toString())).block();
})).and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))).andNest(path("/actor"), route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest.bodyToMono(Actor.class).doOnNext(actors::add).then(ok().build()))).filter((request, next) -> {
System.out.println("Before handler invocation: " + request.path());
return next.handle(request);
});
}
Aggregations