【问题标题】:Webflux error - no instance(s) of type variable(s) R exists so that Flux<UUID> conforms to Mono<>Webflux 错误 - 不存在类型变量 R 的实例,因此 Flux<UUID> 符合 Mono<>
【发布时间】:2020-11-04 06:43:17
【问题描述】:

我使用 Spring Boot 和 Webflux。

在我的代码中,我尝试返回 Flux,但我从 intellij 收到以下错误并且代码标记为红色:

no instance(s) of type variable(s) R exists so that Flux<UUID> conforms to Mono<>

我的代码:

public Flux<UUID> deleteCCProtections(Mono<ProtectionSetRequest> protectionSetRequest) {
        return protectionSetRequest.flatMap(
            request ->
                protectionSetRepository
                    .findByProtectionSetIdIn(request.getProtectionSetIds())
                    .collectList()
                    .flatMap(
                        protectionSet ->  //this line is marked red in Intellij
                            deleteCCProtectionset(protectionSet, request.getDeleteBackups()))); //this line is marked red in Intellij
    }
    
    private Flux<UUID> deleteCCProtectionset(
        List<ProtectionSet> protectionSets, boolean deleteRecoveryPoints) {
        return Flux.fromIterable(protectionSets)
            .flatMap(
                protectionSet ->
                    protectorRepository
                        .findByProtectionId(protectionSet.getProtectionId())
                        .flatMap(
                            protector ->
                                Mono.zip(
                                    protectBatchService.delete(protector),
                                    protectionSetRepository.delete(protectionSet))
                                    .flatMap(
                                        tuple ->
                                            protectionService.sendUnprotectCommand(
                                                tuple.getT1()))
                                    .doOnNext(subscriptionResourceService::cancelSubscriptionResources)
                                    //
                                    // .doOnNext(agentDataServiceApi::setProtectedResources) //void
                                    .doOnNext(schedulerService::deleteProtection))); //void
    }

我做错了什么?

更新

当我从参数 deleteCCProtections(Mono protectionSetRequest 中删除 Mono 时,我的代码会编译 - 为什么???我将 Mono 从控制器获取到服务...

工作代码但没有 Mono

public Flux<UUID> deleteCCProtections(ProtectionSetRequest protectionSetRequest) {
        return protectionSetRepository
                 .findByProtectionSetIdIn(protectionSetRequest.getProtectionSetIds())
                    .collectList()
                    .flatMapMany(
                        protectionSet ->  //this line is marked red in Intellij
                            deleteCCProtectionset(protectionSet, request.getDeleteBackups()))); //this line is marked red in Intellij
    }
    
    private Flux<UUID> deleteCCProtectionset(
        List<ProtectionSet> protectionSets, boolean deleteRecoveryPoints) {
        return Flux.fromIterable(protectionSets)
            .flatMap(
                protectionSet ->
                    protectorRepository
                        .findByProtectionId(protectionSet.getProtectionId())
                        .flatMap(
                            protector ->
                                Mono.zip(
                                    protectBatchService.delete(protector),
                                    protectionSetRepository.delete(protectionSet))
                                    .flatMap(
                                        tuple ->
                                            protectionService.sendUnprotectCommand(
                                                tuple.getT1()))
                                    .doOnNext(subscriptionResourceService::cancelSubscriptionResources)
                                    //
                                    // .doOnNext(agentDataServiceApi::setProtectedResources) //void
                                    .doOnNext(schedulerService::deleteProtection))); //void
    }

【问题讨论】:

    标签: java spring-boot spring-webflux


    【解决方案1】:

    由于deleteCCProtectionset 返回Flux&lt;UUID&gt;,您应该在deleteCCProtections 方法中使用flatMapMany 而不是flatMap

    public Flux<UUID> deleteCCProtections(Mono<ProtectionSetRequest> protectionSetRequest) {
        return protectionSetRequest.flatMapMany(request -> protectionSetRepository
                .findByProtectionSetIdIn(request.getProtectionSetIds())
                .collectList()
                .flatMapMany(protectionSet -> deleteCCProtectionset(protectionSet, request.getDeleteBackups())));
    }
    

    【讨论】:

    • 当我将 flatMap 更改为 flatMapMany 时,我的代码仍然无法编译 - 这次在方法 deleteCCProtections 中存在以下问题:所需类型 Flux,提供:Mono
    • 您还应该将第一个 flatMap 更改为 flatMapMany,因为现在它还从嵌套的 lambda (request -&gt; ...) 返回 Flux。代码更新
    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 2019-12-16
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多