简体   繁体   English

如何返回包含 Reactive Mono 和 Flux 的 Reactive Flux?

[英]How to return a Reactive Flux that contains a Reactive Mono and Flux?

I'm new to reactive programming and ran into this problem:我是响应式编程的新手,遇到了这个问题:

[
    {
        "customerDTO": {
            "scanAvailable": true
        },
        "bankAccountDTOs": {
            "scanAvailable": true,
            "prefetch": -1
        }
    }
]

DTO: DTO:

public class ResponseClientDTO {
    private Mono<CustomerDTO> customerDTO;
    private Flux<BankAccountDTO> bankAccountDTOs;
}

Service:服务:

public Flux<ResponseClientDTO> getCustomerWithBankAccounts(String customerId){
    Flux<BankAccountDTO> bankAccounts = webClient
        .get()
        .uri(uriBuilder -> 
                uriBuilder.path("customers")
                .queryParam("customerId", customerId).build())
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .bodyToFlux(BankAccountDTO.class);
        
    
    Mono<CustomerDTO> cMono = findOne(customerId);
    
    ResponseClientDTO responseClientDTO = new ResponseClientDTO();
    responseClientDTO.setBankAccountDTOs(bankAccounts);
    responseClientDTO.setCustomerDTO(cMono);
    
    return Flux.just(responseClientDTO);
}

I query an endpoint from another API, and it returns a Flux<BankAccounts> .我从另一个 API 查询一个端点,它返回一个Flux<BankAccounts> I want to get the client with all his bank accounts.我想得到客户的所有银行账户。

That is not what you want in a reactive stack.这不是您在反应式堆栈中想要的。 First, change your DTO so that it does't include Mono and Flux , but CustomerDTO and List<BankAccountDTO> instead:首先,更改您的 DTO,使其不包含MonoFlux ,而是包含CustomerDTOList<BankAccountDTO>

public class ResponseClientDTO {
    private CustomerDTO customerDTO;
    private List<BankAccountDTO> bankAccountDTOs;
}

Then, you need to rearrange your method to return a Mono<ResponseClientDTO> instead and to change the logic to deal with Flux and Mono :然后,您需要重新安排方法以返回Mono<ResponseClientDTO>并更改处理FluxMono的逻辑:

public Mono<ResponseClientDTO> getCustomerWithBankAccounts(String customerId){
    Flux<BankAccountDTO> bankAccounts = webClient
        .get()
        .uri(uriBuilder -> 
                uriBuilder.path("customers")
                .queryParam("customerId", customerId).build())
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .bodyToFlux(BankAccountDTO.class);
        
    Mono<CustomerDTO> cMono = findOne(customerId);
    
    return bankAccounts.collectList().zipWith(cMono).map(data -> {
        ResponseClientDTO responseClientDTO = new ResponseClientDTO();
        responseClientDTO.setBankAccountDTOs(data.getT1());
        responseClientDTO.setCustomerDTO(data.getT2());
    })
}

(Sorry for any Java typo but at this point I am too used to Kotlin). (很抱歉有任何 Java 错字,但此时我已经习惯了 Kotlin)。

Consider taking a look at the following useful online resources:考虑查看以下有用的在线资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Mono 到 Flux 的反应列表 - Spring Reactive List of Mono to Flux 如何在 java 反应式中为通量 / mono 调用多个服务? - How to invoke multiple services for a flux / mono in java reactive? 如何通过Spring WebFlux Reactive方法在处理函数中使用Mono和Flux - How to use Mono and Flux in handler functions with Spring WebFlux Reactive ways Spring Reactive使用FLUX或MONO消耗POST - Spring Reactive consume POST using FLUX or MONO 需要帮助理解反应式 Mono/Flux 类 - Need help understanding reactive Mono/Flux classes 如何迭代 Flux 以产生新的 Flux 并仍然以 Reactive 方式工作? - How to iterate on a Flux to produce a new Flux and still work in a Reactive way? 如何重新分配反应通量 stream 中的变量? - How to reassign a variable in reactive Flux stream? 如何使用Spring Reactive修改助焊剂的结果 - How to modify results from a Flux with Spring Reactive 如何在准备就绪时使用反应式 Flux/Mono 将消息推送到上游而不是在间隔中轮询状态? - How to push message to upstream using reactive Flux/Mono whenever they are ready than polling in interval for status? 如何实现具有可完成未来的具有 Mono/Flux 的 Reactive Framework 中存在的.switchIfEmpty() 类型的方法? - How do I achieve .switchIfEmpty() type of method present in Reactive Framework with Mono/Flux with completable future?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM