gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:45:18 28 4
gpt4 key购买 nike

我开始学习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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com