我开始学习Spring,并在本教程中从中学习,讲师使用以下方法:startAndAwait,该方法位于Reactor.ipc.netty.http.server.HttpServer包中。现在没有方法了,程序包是react.netty.http.server.HttpServer。

我想学习基于最新软件包的解决方案,因此,我的问题是,以下代码的当前等效内容是什么:

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.ipc.netty.http.server.HttpServer;


import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

public class HelloServerApplication {

    public static void main(String[] args)
    {
        RouterFunction route = route( GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);

        HttpServer server = HttpServer.create("localhost",8080);
        server.startAndAwait(new ReactorHttpHandler(httpHandler));
    }

}


我一直在寻找解决方案,但是我的知识如此之低,以至于我无法独自应对这个问题。到目前为止,我已经写了将代码更改为“ server.startAndAwait”的位置,但仍然无法替换此方法:

package pl.javasurvival.helloServer;

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;


import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

public class HelloServerApplication {

    public static void main(String[] args)
    {
        RouterFunction route = route( GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);

        HttpServer server = HttpServer
                .create()
                .host("localhost")
                .port(8080);

        //what is a new method which is equals to startAndAwait

    }

}


PS:我忘了补充说,我使用gradle。这是build.gradle文件:

plugins {
    id 'org.springframework.boot' version '2.2.0.M4'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'pl.javasurvival'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux:2.2.0.M4'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        exclude group: 'junit', module: 'junit'
    }
    testImplementation 'io.projectreactor:reactor-test'
}

test {
    useJUnitPlatform()
}

最佳答案

已经有一段时间了,但是1小时前我发现了这个问题,现在我有了解决方案,因此对其他人可能会有帮助。

无需导入旧版本的react.netty,您可以尝试执行此操作(添加扫描程序仅用于等待操作)

import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;

import java.util.Scanner;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;


public class HelloServerApplication {

    public static void main(String[] args) {
        RouterFunction route = route(GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));

        var httpHandler = RouterFunctions.toHttpHandler(route);

        var adapter = new ReactorHttpHandlerAdapter(httpHandler);

        var server = HttpServer.create().host("localhost").port(8080).handle(adapter).bindNow();

        System.out.println("press enter");

        Scanner sc = new Scanner(System.in);
        sc.next();
        server.disposeNow();
    }
}

09-16 03:55