在Spring-Boot 2.0 REST控制器中,我创建了以下代码,该代码可根据需要工作:
@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();
}
以下位置:
@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&lt;LIST&LT;字符串>>到发布者&lt;? 扩展出版商&lt;?扩展对象>>
所以一些问题:
- 我以为流通是出版商?那么为什么错误?
- 我在测试4中做错了什么,以便输出与Test3中相同的结果?
预期的输出为:[["一个","两个","三","四","五","六"]]
使用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();
}