我正在嘗試在專案反應堆中再次做一些事情,我敢肯定這對你們那里的任何專案反應堆大師來說都很簡單!我一直在尋找和摸索這個東西一段時間,感覺我又一次用這些東西撞墻了。
我要做的就是確定 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())));
}
與往常一樣,非常感謝任何和所有幫助和見解!
uj5u.com熱心網友回復:
我不確定這是否能解決問題,但這樣您就可以嘗試撰寫邏輯
return accountService.getAccount(accountId)
.map(Account::getClientId)
.map(clientId -> createUpdateCardStatusServiceRequestData(clientId, cardId, cardStatus))
.flatMap(requestData -> cartaClient.updateCardStatus(requestData)
.then(Mono.defer(() ->
Mono.zip(
Mono.just(isCardBeingCancelled(cardStatus)),
isLastCardForAccount(accountId),
Mono.just( isLostOrStolen(cardStatus) )
)
.map(tuple -> {
WRITE YOUR IF ELSE LOGIC
})
這個想法是使用 zip,然后使用元組來撰寫邏輯。元組將是 <Boolean, Boolean ,Boolean> 的 Tuple3 型別。我假設 isLostOrStolen(cardStatus) 回傳布林值。
uj5u.com熱心網友回復:
一種方法是使用這樣的filterWhen運算子:
.then(Mono.defer(() -> {
if (isCardBeingCancelled(cardStatus)) {
return Mono.just(accountId)
.filterWhen(this::allCardsCancelledForAccount)
.flatMap(this::removeAccount);
} else if (isLostOrStolen(cardStatus)) {
return replaceCard(cardId, cardStatus).flatMap(this::updateCardNumber);
}
return Mono.empty();
}))
您可以filterWhen在異步過濾的情況下使用。查看我需要哪個運營商的這一部分?參考和這個如何異步過濾 Flux。
作為旁注,這不會像您期望的那樣作業:
private Mono<Boolean> isLastCardForAccount(String accountId) {
return cardService.getAccountCards(accountId)
.hasElement();
}
public Mono<List<Card>> getAccountCards(final String accountId) {
return cardCrudRepository.getCardsByAccountId(accountId)
.collectList();
}
如果沒有卡,collectList()將發出一個空List值。我會改用exists查詢:
public Mono<Boolean> isLastCardForAccount(final String accountId) {
return cardCrudRepository.existsByAccountId(accountId);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/398993.html
標籤:春天 弹簧靴 反应式编程 弹簧-webflux 项目反应堆
上一篇:當另一個表已經存在時如何使用外鍵進行POST-使用spring
下一篇:公開暴露API端點
