Netty HttpServer api has changed / differs from available examples

Implementing a Netty instance in an Arjen Poutsma and Josh Long blog entry video is done by creating an instance reactor.ipc.netty.http.HttpServerand then calling a method startor startAndAwaitwith an instance ReactorHttpHandlerAdapteras an argument.

However, the API, it seems, has changed because now the methods startand startAndAwaitnow expect a lambda with the following signature:

java.util.function.Function<? super reactor.ipc.netty.http.HttpChannel,? extends org.reactivestreams.Publisher<java.lang.Void>>

Project dependencies and versions are the same as in the Arjen Poutsma project example

    <dependency>
        <groupId>org.reactivestreams</groupId>
        <artifactId>reactive-streams</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.ipc</groupId>
        <artifactId>reactor-netty</artifactId>
        <version>0.5.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>8.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web-reactive</artifactId>
        <version>5.0.0.BUILD-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.2</version>
    </dependency>

What is the new / correct way to instantiate a netty server with spring support?

0
source share
1

http://start.spring.io/, . , spring - , samples.This :

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-dependencies-web-reactive</artifactId>
            <version>0.1.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

HttpServer :

import org.reactivestreams.Publisher;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.RouterFunction;
import org.springframework.web.reactive.function.ServerRequest;
import org.springframework.web.reactive.function.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.http.server.HttpServer;

import java.io.IOException;

import static org.springframework.web.reactive.function.RequestPredicates.GET;
import static org.springframework.web.reactive.function.RouterFunctions.route;
import static org.springframework.web.reactive.function.RouterFunctions.toHttpHandler;

public class FunctionalReactiveServer {

    public static final String HOST = "localhost";
    public static final int PORT = 8080;

    public static void main(String[] args) throws InterruptedException, IOException {
        RouterFunction<?> route = route(GET("/sayHello"), FunctionalReactiveServer::sayHelloHandler);
        HttpHandler httpHandler = toHttpHandler(route);

        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer server = HttpServer.create(HOST, PORT);
        server.newHandler(adapter).block();

        System.out.println("Press ENTER to exit.");
        System.in.read();
    }

    public static ServerResponse<Publisher<String>> sayHelloHandler(ServerRequest request) {
        return ServerResponse.ok().body(Mono.just("Hello!"), String.class);
    }

}
+1

Source: https://habr.com/ru/post/1660324/


All Articles