Mono::then returns 空

Mono::then returns null

我对响应式编程比较陌生。 我的问题是关于 Mono::then

我想做的是,从传入请求中提取主体,将其设置为静态变量。 完成后,发送一个响应,说明服务已启动。 但是下面的代码总是returns "Service started for: null".

我认为 Mono::then 应该 运行 在第一个单声道完成后(在这种情况下,在设置静态变量之后)和 return "Service started for: a,b,c".

我的理解有误吗?

(此外,欢迎任何代码批评)

public Mono<ServerResponse> getStatus(ServerRequest req) {
        Mono<List<Request>> body = req.bodyToFlux(Request.class).collectList();
        return ServerResponse.ok()
                .body(body.doOnNext(i -> {
                    Service.a = i;
                    logger.info("Service started for : {}", i.toString());
                })


                        .then(Mono.just("Service started for: " + Service.a)), String.class);
    }

非常不鼓励通过静态变量进行通信(尤其是在函数式和反应式编程中)。由于在你的问题中你没有提供足够的关于你开始的 Service 的信息,所以有点难以推荐。

但是,根据可用的信息,我会从这样的事情开始:

public Mono<ServerResponse> getStatus(ServerRequest req) {
    return req.bodyToFlux(Request.class)
            .collectList()
            .doOnNext(requestBody -> System.out.println("Do your service start here in the background."))
            .flatMap(requestBody -> ServerResponse.ok().syncBody("Service started for: " + requestBody));
}