You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flux<ObjectA>转Mono<ObjectB>编译报错求助

解决编译错误的方法

错误原因

编译报错的核心问题是:stream().flatMap() 要求映射函数返回 Stream类型,但你直接传入了r.getSuccess()/r.getFailure()(返回的是List<String>),类型不匹配导致泛型推导失败。

修正后的代码

public Mono<ObjectB> controllerMethod() {
    return service.serviceMethod()
        .collectList()
        .map(res -> ObjectB.builder()
                .completed(res.stream()
                        .flatMap(r -> r.getSuccess().stream()) // 将List转为Stream后再扁平化
                        .collect(Collectors.toList()))
                .unCompleted(res.stream()
                        .flatMap(r -> r.getFailure().stream()) // 同样处理failure列表
                        .collect(Collectors.toList()))
                .build());
}

关键修改点

  • 将 r.getSuccess() 改为 r.getSuccess().stream(),把List转换成Stream,满足flatMap的参数要求
  • 将 r.getFailure() 改为 r.getFailure().stream(),同理处理失败列表
  • 这里不需要用flatMap,直接用map即可(因为collectList()返回的是Mono<List<ObjectA>>,map就能同步处理这个列表,flatMap适用于异步场景)

内容的提问来源于stack exchange,提问作者Kaigo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:42:35