为什么不能在反应堆网络中订阅请求?

问题描述

我只想访问react-netty项目中的Http内容。但是结果为

代码在下面。

DisposableServer server =
            HttpServer.create()
                    .host("localhost")
                    .port(8000)
                    .route(routes ->
                                    .post("/echo",(request,response) -> 
                                                {   request.receive()
                                                        .retain()
                                                        .aggregate()
                                                        .asString()
                                                        .subscribe(System.out::println);
                                                    return response.sendString(Mono.just("hello"));})          
                    .bindNow();

我无法在控制台中获得结果。

我可以像在代码中一样访问请求吗? 有人可以帮忙吗?谢谢。

解决方法

您在接收到请求数据之前返回响应,因此Reactor Netty将删除在发送响应后接收到的所有传入数据。

我不知道您的用例,但是将示例更改为下面的示例,您将能够看到传入的数据:

DisposableServer server =
        HttpServer.create()
                .host("localhost")
                .port(8000)
                .route(routes ->
                        routes.post("/echo",(request,response) ->
                                        response.sendString(request.receive()
                                                .retain()
                                                .aggregate()
                                                .asString()
                                                .flatMap(s -> {
                                                    System.out.println(s);
                                                    return Mono.just("hello");
                                                })))
                )
                .bindNow();

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...