如何结合 Flux 和 Mono

How to combine Flux and Mono

假设我们有以下函数

fun getAnnualData(tenant: String): Flux<DashboardResponse> {

    val year = LocalDate.now().year

    val annualExpenses = expenseFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
    val warehouseExpenses = expenseFinder.sumWarehouseByYearAndTenant(year = year, tenant = tenant)
    val annualRevenues = revenueFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
    
    return annualExpenses.zipWith(annualRevenues)
        .filter { it.t1._id?.year == year }
        .filter { it.t2._id?.year == year }
        .map {
            DashboardResponse(
                period = Period.ANNUAL,
                expenses = it.t1,
                revenue = it.t2
            )
        }
}

并且我想将 warehouseExpenses 添加到返回值中。我该怎么做?

在哪里

您可以简单地使用 flatMap 来组合 Mono:

Flux.just(...).zipWith(Flux.just(...))
        .filter(...)
        .flatMap(tuple -> Mono.just(true).map(m -> new DashboardResponse(tuple.getT1(), tuple.getT2(), m)));