使用 webflux 在另一个单声道中设置一个单声道

Setting one mono within another using webflux

我正在从 mongo db

中检索发布者 Mono<ProductOrder>

我有另一个发布者Mono<CommandResponse>

我想将 ProductOrder 对象设置为 CommandResponse,因为它是 class 的一部分。喜欢 commandResponse.setProductOrder(po instance)

CommandResponse 也会有不同的 MonoString or Int 除了 ProductOrder 实例

最后想要 return Mono<ServerResponse> 其中包含 CommandResponse

的正文

我无法将 Mono<ProductOrder> 对象设置为 Mono<CommandResponse>

请帮忙。谢谢。


代码片段

@Component
public class Handler {


    @Autowired
    private MongoService service;

    public Mono<ServerResponse> get(ServerRequest request) {
        Mono<ProductOrder> p = service.queryById();
        
        

> Here, in the return statement i want to return Mono<CommandResponse> 
> instead of Mono<ProductOrder> in the response body
> remember: CommandResponse has a reference to ProductOrder

        return 
            ServerResponse.ok()
                .contentType(MediaType.APPLICATION_JSON)
                .body(p, ProductOrder.class).map(null);
        );
    }
}
Mono<CommandResponse> commandMono = ... // I don't how it is set 
Mono<ProductOrder> productMono = service.queryById();
Mono.zip(commandMono, productMono)
  .map(tuple -> { 
    var command = tuple.getT1();
    var product = tuple.getT2();
    command.setProductOrder(product) // => .setProductOrder(po instance)
    return command;
  })
  .flatMap(command -> ServerResponse.ok()
    .contentType(MediaType.APPLICATION_JSON)
    .bodyValue(command)
  )

我没有运行这段代码,所以我不确定它是否可以编译,但是有了它你应该能够弄清楚你的代码是如何工作的