1

在 spring-boot 2.0 休息控制器中,我创建了以下代码,可以按需要工作:

@ResponseBody
@GetMapping("/test3")
Mono<List<String>> test3(){
    List<String> l1 = Arrays.asList("one","two","three");
    List<String> l2 = Arrays.asList("four","five","six");

    return Flux
               .concat(Flux.fromIterable(l1),Flux.fromIterable(l2))
               .collectList();
}

我的问题来自尝试从外部数据源做同样的事情。我创建了以下测试用例:

@ResponseBody
@GetMapping("/test4")
Flux<Object> test4(){
    List<String> indecies = Arrays.asList("1","2");
    return Flux.concat(
            Flux.fromIterable(indecies)
        .flatMap(k -> Flux.just(myRepository.getList(k))
                          .subscribeOn(Schedulers.parallel()),2
                )
        ).collectList();
}

其中 myRepository 如下:

@Repository
public class MyRepository {

List<String> l1 = Arrays.asList("one","two","three");
    List<String> l2 = Arrays.asList("four","five","six");
    Map<String, List<String>> pm = new HashMap<String, List<String>>();

MyRepository(){
    pm.put("1", l1);
    pm.put("2", l2);
}

List<String> getList(String key){
    List<String> list = pm.get(key);
    return list;
}   
}

我标记为 test4 的代码给了我代码提示错误:

类型不匹配:无法从 Flux< List < String >> 转换为 Publisher < ? 扩展发布者 < ? 扩展对象>>

所以有几个问题:

  1. 我以为 Flux 是出版商?那么为什么会出错呢?
  2. 我在测试 4 中做错了什么,以至于它会输出与 test3 中相同的结果?

预期的输出是:[["one","two","three","four","five","six"]]

4

1 回答 1

3

使用 M. Deinum 的评论,这是有效的:

@ResponseBody
@GetMapping("/test6")
Mono<List<String>> test6(){
    List<String> indecies = Arrays.asList("1","2");

    return Flux.fromIterable(indecies)
               .flatMap(k -> Flux.fromIterable(myRepository.getList(k)).subscribeOn(Schedulers.parallel()),2)
               .collectList();

}
于 2017-06-23T21:49:04.823 回答