【问题标题】:Spring Reactive: manipulate a flux into a mono type responseSpring Reactive:将通量操纵为单声道响应
【发布时间】:2021-09-01 02:24:52
【问题描述】:

我对 Spring Reactive 还很陌生。

我想将 Spring Reactive Repo Flux 响应转换为另一种类型的 Mono 类型响应:

@Transactional(readOnly = true)
@Override
public Mono<BaseListResponseVm<AcsTransactionVm>> transactionsBy(
        @NonNull Integer offset,
        @NonNull Integer limit,
        @NonNull String term,
        AcsTransactionRequestDto dto
) {
    var response = new BaseListResponseVm<AcsTransactionVm>();
    var list = repository.findByPan(term, PageRequest.of(offset, limit));

    return repository.findByPan(term, PageRequest.of(offset, limit))
            XXXXX.(res -> {
                response.setItems(); // List<AcsTransactionVm>
                response.setCount(); // Long
            });
}

存储库

public interface AcsTransactionRepository extends ReactiveCassandraRepository<AcsTransactionDao, Integer> {

    @AllowFiltering
    Flux<AcsTransactionDao> findByPan(String term, Pageable page);

}

我已经尝试过提供的运算符(转换是我需要的关闭,但它仍然将回购结果的每个参数都提供为反应性参数),但最终无法得到任何我知道的结果如何使用。

【问题讨论】:

标签: java spring spring-webflux reactive


【解决方案1】:

这似乎是这样做的方法:

@Transactional(readOnly = true)
@Override
public Mono<BaseListResponseVm<AcsTransactionVm>> transactionsBy(
        @NonNull Integer offset,
        @NonNull Integer limit,
        @NonNull String term,
        AcsTransactionRequestDto dto
) {
    log.debug("DB: fetching ACS transactions using offset {}, limit {}, term {} and request {}", offset, limit, term, dto);
    var response = new BaseListResponseVm<AcsTransactionVm>();

    return repository.findByPan(term, PageRequest.of(offset, limit))
            .collectList().map(transactions -> {
                var mappedItems = transactions.stream()
                        .map(mapper::toVm)
                        .collect(Collectors.toList());
                response.setItems(mappedItems);

                return response;
            });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2023-01-26
    • 2021-05-24
    • 2021-03-09
    • 2019-10-06
    相关资源
    最近更新 更多