【问题标题】:Spring-Webflux:Working with two Mono ResponsesSpring-Webflux:使用两个 Mono 响应
【发布时间】:2021-02-17 01:16:23
【问题描述】:

我正在尝试评估来自两个不同服务的 Mono 类型的响应

data class Customer(
        val customerId : String ,
        val customerBlance : CustomerBalance,
        val customername : String ,
        val customerRefNo : String
)


data class CustomerBalance(
        val totalAmount : Double ,
        val totalCreditAmount : Double ,
        val totalDebitAmount : Double
)

这里我有两个服务,一个返回客户信息,另一个返回客户余额 都发出 Mono 的对象。

firstService 返回我需要在 CustomerBalance 对象上设置的 Mono。

第二个服务返回我需要在 CustomerBalance 对象上设置的 Mono。

我尝试编写下面的代码能够评估第一个单声道,但在迭代第二个单声道以设置 CustomerBalance 对象时遇到问题。

 class CustomerInformationService
   {
        fun getCustomerInfomation( firstObject:Mono<Any>, second:Mono<Any>):Mono<Customer>
        {
                second.map { getCustomerInfo(it) }
        }

    private fun getCustomerInfo(it : Any?):Customer {
            return Customer()
    }
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java kotlin mono reactive-programming spring-webflux


    【解决方案1】:

    不完全确定我是否遵循您的操作,但通常在响应式流程中,您不会传递 Monos 而是传递实际值,然后将不同的服务调用链接在一起。获得所需的所有信息后,您可以将其映射到您希望返回的对象。

    所以你会写这样的东西:

    fun getCustomerInformation(customerId: String) :Mono<Customer> {
        return customerId.toMono()
            .flatMap { getCustomerInfo(it) }
            .flatMap { getCustomerBalance(it) }
            .map { ObjectYouWantToReturn }
    }
    

    【讨论】:

      【解决方案2】:

      我使用Mono.zipWith(Mono)解决了这个问题

      【讨论】:

        猜你喜欢
        • 2020-10-08
        • 2020-09-18
        • 2021-11-18
        • 2020-10-14
        • 2018-12-19
        • 2018-12-12
        • 1970-01-01
        • 2018-09-21
        • 2022-01-02
        相关资源
        最近更新 更多