java - 哪个相当于reactor.netty.http.server包中的旧方法startAndAwait?

标签 java spring http netty httpserver

我开始学习Spring,在我学习的教程中,讲师使用了方法:startAndAwait,该方法位于reactor.ipc.netty.http.server.HttpServer包中。现在没有方法了,包是reactor.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()
}

最佳答案

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

如果不导入旧版本的reactor.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();
    }
}

关于java - 哪个相当于reactor.netty.http.server包中的旧方法startAndAwait?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57150028/

相关文章:

ruby - 用完 Net::HTTP 的套接字连接

带有自定义 header 的 PHP POST 给出 HTTP 400 错误请求响应(错误?)

java - 删除链接列表中第一次出现的项目

java - KafkaProducer 连接被拒绝

java - memchache 不工作时如何处理代码(simple-spring-memcached)

java - Spring ApplicationContext 未缓存用于与 Maven 的集成测试

java - 来自 Autowiring 对象的 spring 变量注入(inject)

java - 所有 Spring beans 都是代理的吗?

java - 仅更新 hibernate 中对象的非空字段

Java REST API : Can not deserialize instance of Object out of START_ARRAY token