【发布时间】:2021-12-27 22:03:06
【问题描述】:
我正在尝试在 project reactor 中再次做一些事情,我相信这对你们中的任何一个 project reactor 专家来说都是非常简单的! 我一直在寻找和摸索这个东西有一段时间了,感觉我又一次碰壁了。
我要做的就是确定 Mono 中包含的对象列表是否为空。
这是我目前所拥有的:
private Mono<Boolean> isLastCardForAccount(String accountId) {
return cardService.getAccountCards(accountId)
.hasElement();
}
我认为上述方法可能有效,但我很难弄清楚如何提取/访问返回的 Mono 中包含的“布尔值”。我想我必须以某种方式使用“订阅”,对吗? 我已经搞砸了一段时间了,但仍然没有运气。
以下是“getAccountCards”的定义方式:
public Mono<List<Card>> getAccountCards(final String accountId) {
return cardCrudRepository.getCardsByAccountId(accountId)
.collectList();
}
来自 CardCrudRepository:
// @Query("SELECT * FROM card WHERE account_id = :accountId") <-Not sure if I need this
Flux<Card> getCardsByAccountId(String accountId);
最后,我如何使用“isLastCardForAccount”:
public Mono<Void> updateCardStatus(String accountId, String cardId, String cardStatus) {
return accountService.getAccount(accountId)
.map(Account::getClientId)
.map(clientId -> createUpdateCardStatusServiceRequestData(clientId, cardId, cardStatus))
.flatMap(requestData -> cartaClient.updateCardStatus(requestData)
.then(Mono.defer(() -> isCardBeingCancelled(cardStatus) ? allCardsCancelledForAccount(accountId) ? removeAccount(accountId) :
(isLostOrStolen(cardStatus) ? replaceCard(cardId, cardStatus).flatMap(this::updateCardNumber) : Mono.empty()) : Mono.empty())));
}
与往常一样,我们非常感谢任何和所有帮助和见解!
【问题讨论】:
-
isCardBeingCancelled(cardStatus) 返回布尔值或单声道
?? -
检查此代码。你会有一些想法
Flux<Integer> integerFlux = Flux.just(1,2,3,4,5,6);integerFlux.count().subscribe(System.out::println);integerFlux.collectList().map(list->list.size()).subscribe(System.out::println); -
嗨@Harry。这是回答您问题的代码: private boolean isCardBeingCancelled(String cardStatus) { return isCardDamagedFraudulentOrBeingBlocked(cardStatus); } 所以答案是“布尔值”。
-
谢谢@AjitSoman。我会看看我是否可以以某种方式使用该代码。
-
您能用基本的英语解释一下您在
then运算符中尝试实现的逻辑吗?很难跟上
标签: spring spring-boot reactive-programming spring-webflux project-reactor