我使用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来服务…
工作代码,但没有单声道
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
}
由于deleteCCProtectionset
返回Flux<UUID>
,因此在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())));
}