Search in sources :

Example 1 with GET

use of org.springframework.web.reactive.function.server.RequestPredicates.GET in project spring-framework by spring-projects.

the class HttpServerTests method setUp.

@Before
public void setUp() throws Exception {
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route(GET("/test"), request -> ServerResponse.ok().body(Mono.just("It works!"), String.class)));
    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();
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) After(org.junit.After) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Before(org.junit.Before) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.http.server.reactive.bootstrap.ReactorHttpServer) Before(org.junit.Before)

Example 2 with GET

use of org.springframework.web.reactive.function.server.RequestPredicates.GET 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 3 with GET

use of org.springframework.web.reactive.function.server.RequestPredicates.GET 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)

Example 4 with GET

use of org.springframework.web.reactive.function.server.RequestPredicates.GET in project spring-framework by spring-projects.

the class HttpServerTests method start.

@BeforeEach
public void start() throws Exception {
    HttpHandler httpHandler = RouterFunctions.toHttpHandler(route(GET("/test"), request -> ServerResponse.ok().bodyValue("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();
}
Also used : Test(org.junit.jupiter.api.Test) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) AfterEach(org.junit.jupiter.api.AfterEach) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) GET(org.springframework.web.reactive.function.server.RequestPredicates.GET) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) RouterFunctions.route(org.springframework.web.reactive.function.server.RouterFunctions.route) HttpHandler(org.springframework.http.server.reactive.HttpHandler) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

HttpHandler (org.springframework.http.server.reactive.HttpHandler)4 GET (org.springframework.web.reactive.function.server.RequestPredicates.GET)4 RouterFunctions (org.springframework.web.reactive.function.server.RouterFunctions)4 ServerResponse (org.springframework.web.reactive.function.server.ServerResponse)4 RouterFunctions.route (org.springframework.web.reactive.function.server.RouterFunctions.route)3 Mono (reactor.core.publisher.Mono)3 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)2 POST (org.springframework.web.reactive.function.server.RequestPredicates.POST)2 RouterFunction (org.springframework.web.reactive.function.server.RouterFunction)2 Flux (reactor.core.publisher.Flux)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 After (org.junit.After)1 Before (org.junit.Before)1 Test (org.junit.Test)1 AfterAll (org.junit.jupiter.api.AfterAll)1