【问题标题】:Convert the Mono<List<PojoA>> to List<PojoB> [closed]将 Mono<List<PojoA>> 转换为 List<PojoB> [关闭]
【发布时间】:2021-01-14 18:30:21
【问题描述】:

我有一个Mono&lt;List&lt;PojoA&gt;&gt; 对象。我需要迭代PojoA and 的列表形成一个新的List&lt;String&gt;

    public List<String> getImageList() {
    
        Mono<List<PojoA>> pojoAListMono = someMethod();
        List<String> list = new ArrayList<String>();
    
        pojoAListMono.flatMapMany(imageList -> {
          imageList.stream().forEach(image -> list.add("/images/" + image.getImageName()));
        });
      }

【问题讨论】:

标签: spring-webflux project-reactor flux reactive


【解决方案1】:

你要求做什么(从一个流到一个对象列表) 不建议这样做,因为您失去了将反应式(异步)转换为阻塞状态的能力。

但确定你可以使用.block() 下面是一个

            public List<String> getImageList() {
               return someMethod()
               .flatMapIterable(pojoAS -> pojoAS)
               .map(pojoA -> "/images/"+pojoA.getImageName())
               .collectList()
               .block();
            }
               

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 2022-01-11
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多