首页 > 解决方案 > 转换单声道> 列出

问题描述

我有一个Mono<List<PojoA>>对象。我需要迭代PojoA and一个新的表单列表List<String>

    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-webfluxproject-reactorfluxreactive

解决方案


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

但确定你可以做到这一点 using.block() 下面是

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

推荐阅读