第一个单声道完成后致电单声道并返回第一个单声道的结果

发布于 2025-02-09 06:34:11 字数 512 浏览 3 评论 0 原文

我想在WebFlux环境中执行两个业务操作,仅在第一个操作成功之后,第二次操作才会发生。第二个完成后,我想返回第一个操作的结果。第二个操作称为org.springframework.web.reactive.function.client.webclient。这是我到目前为止尝试的:

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   service.operation2();
                   return resource;
           })
          .map(ResponseEntity::ok);

}

当时我还尝试了并订阅,但是我无法让网络客户执行​​呼叫并返回service.operation.operation1。我该怎么办?

I want to perform two business operations, in a Webflux environment, in a way that the second operation happens only after the first one is succesfull. After the second one is done, I want to return the result of the first operation. The second operation calls a org.springframework.web.reactive.function.client.WebClient. This is what I have tried until now:

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   service.operation2();
                   return resource;
           })
          .map(ResponseEntity::ok);

}

I also tried with then and subscribe but I can't get the webclient to perform the call and return the result of service.operation1. What must I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稳稳的幸福 2025-02-16 06:34:11

您需要使用反应式操作员构建流程,并让WebFlux订阅它。在您的片段中,没有订阅 service.operation2()

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   return service.operation2()
                       .thenReturn(resource);
           })
          ...

}

You need to construct a flow using reactive operators and let WebFlux subscribe to it. In your snippet there is no subscription to service.operation2()

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .flatMap(resource -> {
                   return service.operation2()
                       .thenReturn(resource);
           })
          ...

}
只是偏爱你 2025-02-16 06:34:11

正确的成语正在调用 mono.delayuntil()

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .delayUntil(service.operation2())
          ...
}

The correct idiom is calling Mono.delayUntil():

enter image description here

Your code is then as simple as:

public Mono<ResponseEntity<Resource>> callOperations(){
   return service.operation1()
          .delayUntil(service.operation2())
          ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文