在 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 < ? 扩展发布者 < ? 扩展对象>>
所以有几个问题:
- 我以为 Flux 是出版商?那么为什么会出错呢?
- 我在测试 4 中做错了什么,以至于它会输出与 test3 中相同的结果?
预期的输出是:[["one","two","three","four","five","six"]]