我如何使用Mono 作为调用第二个方法的条件

问题描述 投票:0回答:2

我正在检查另一个服务的条件后尝试拨打一个服务的电话

以迭代的方式,我可以做到这一点

if (productService.isProductNotExcluded(product)){
    List<Properties> properties = propertiesService.getProductDetailProperties(product)
...
}

但是由于isProductExcluded返回Mono<Boolean>,所以我正在采用这种方法,这似乎很奇怪。

Flux<Properties> properties = productService.isProductNotExcluded(productId)
     .filter(notExcluded -> notExcluded)
     .map(ok-> propertiesService.getProductDetailProperties(product))
     ...

解决这种情况的正确方法是?

spring-webflux reactor
2个回答
0
投票
[您正在做的事情并不奇怪,如果可以避免的话,我个人不会在反应函数Mono<Boolean>中返回布尔值,但它没有错,有时您没有选择的余地。

为了清晰起见,我个人在地图上会有if else声明。我将更改函数的名称,并重写isNot部分。


0
投票
对于返回Mono<Boolean>的谓词,您也可以使用将发布者作为谓词的filterWhen。像这样的东西:
© www.soinside.com 2019 - 2024. All rights reserved.