如何解决,调用Mono<Token>然后结果将用于另一个Mono<Collection>,然后return Mono<collection>?

How to solve the, calling for Mono<Token> then the result will be used to another Mono<Collection>, which will then return Mono<collection>?

嗨,我刚开始学习响应式编程

我这里有这段代码,我这里的流程应该是 我将调用 tokenRepository 获取令牌,然后使用 token.getAccessToken() 作为 cardRepository.findAllCards()

上的参数
public class CardService {

    private final CardRepository cardRepository;
    private final TokenRepository tokenRepository;

    public CardService(CardRepositorycardRepository,
                       TokenRepository tokenRepository) {
        this.cardRepository = cardRepository;
        this.tokenRepository = tokenRepository;
    }

    public Mono<CardCollection> findAllCards(MultiValueMap<String, String> queryParams) {
        Mono<Token> token =tokenRepository.requestToken(); 

        // then I would like to use the token.getAccessToken
        return cardRepository.findAllCards(token.getAccessToken, queryParams); // Then this should return Mono<CardCollection>
    }
}

想知道这是否可行?

我找到了答案,虽然我不太确定这是否是正确的方法。

How to pass data down the reactive chain

这就是我对代码所做的。

public Mono<CardCollection> findAllCards(MultiValueMap<String, String> queryParams) {
  return tokenRepository.requestToken().flatMap(token -> {
      return cardRepository.findAllCards(token.getAccessToken(), queryParams);
  });
}