Search in sources :

Example 1 with RouterFunction

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();
}
Also used : ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) POST(org.springframework.web.reactive.function.server.RequestPredicates.POST) Mono(reactor.core.publisher.Mono) AfterAll(org.junit.jupiter.api.AfterAll) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) BeforeAll(org.junit.jupiter.api.BeforeAll) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) Duration(java.time.Duration) Task(com.baeldung.web.reactive.Task) NettyContext(reactor.ipc.netty.NettyContext) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Task(com.baeldung.web.reactive.Task) HttpServer(reactor.ipc.netty.http.server.HttpServer) ReactorHttpHandlerAdapter(org.springframework.http.server.reactive.ReactorHttpHandlerAdapter) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with RouterFunction

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);
    });
}
Also used : Arrays(java.util.Arrays) RouterFunctions.toHttpHandler(org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler) RequestPredicates.path(org.springframework.web.reactive.function.server.RequestPredicates.path) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) ClassPathResource(org.springframework.core.io.ClassPathResource) POST(org.springframework.web.reactive.function.server.RequestPredicates.POST) ServerResponse.ok(org.springframework.web.reactive.function.server.ServerResponse.ok) MultiValueMap(org.springframework.util.MultiValueMap) ServletHttpHandlerAdapter(org.springframework.http.server.reactive.ServletHttpHandlerAdapter) Mono(reactor.core.publisher.Mono) WebHandler(org.springframework.web.server.WebHandler) BodyExtractors.toDataBuffers(org.springframework.web.reactive.function.BodyExtractors.toDataBuffers) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) BodyExtractors.toFormData(org.springframework.web.reactive.function.BodyExtractors.toFormData) HttpHandler(org.springframework.http.server.reactive.HttpHandler) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) BodyInserters.fromObject(org.springframework.web.reactive.function.BodyInserters.fromObject) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) MultiValueMap(org.springframework.util.MultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

HttpHandler (org.springframework.http.server.reactive.HttpHandler)2 GET (org.springframework.web.reactive.function.server.RequestPredicates.GET)2 POST (org.springframework.web.reactive.function.server.RequestPredicates.POST)2 RouterFunction (org.springframework.web.reactive.function.server.RouterFunction)2 RouterFunctions (org.springframework.web.reactive.function.server.RouterFunctions)2 ServerResponse (org.springframework.web.reactive.function.server.ServerResponse)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 Task (com.baeldung.web.reactive.Task)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AfterAll (org.junit.jupiter.api.AfterAll)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 ReactorHttpHandlerAdapter (org.springframework.http.server.reactive.ReactorHttpHandlerAdapter)1 ServletHttpHandlerAdapter (org.springframework.http.server.reactive.ServletHttpHandlerAdapter)1 MultiValueMap (org.springframework.util.MultiValueMap)1