【发布时间】:2019-12-14 21:02:54
【问题描述】:
我正在尝试从另一个微服务中获取数据。假设您有三个微服务:State、School 和 Student。您通过来自 SchoolRepository 的 stateId 获得 Flux
public Flux<School> getBySchool(Long stateId){
Flux<School> schoolList=schoolRepository.findByStateId(stateId);
//And for each school I want to do this
Flux<Student> studentsfound=webClient.get().uri("bla bla bla"+school.getSchoolId).exchange().flatMapMany(response->response.bodyToFlux(Student.class));
//I have a List<Student> entity in School domain, so I want Flux<Student> --> List<Student> and add it to School. Something like school.setStudentList(studentListReturned).
//And then return Flux<Stundent>
}
如何遍历 Flux ,获得 Flux 后如何将其添加到适当的 Flux ?提前谢谢你。
更新
解决方案
非常感谢@K.Nicholas。我能够解决以下问题,但欢迎使用更优雅的解决方案。我在控制器中订阅了 schoolList,因为我必须将 Flux
public Flux<School> getBySchoolWithStudents(Long stateId) {
Flux<School> schoolList = schoolRepository.findByStateId(stateId);
return schoolList.flatMap(school -> {
Flux<Student> studentFlux = webClientBuilder.build().get().uri(REQUEST_URI + school.getSchoolId()).exchange().flatMapMany(response -> response.bodyToFlux(Student.class));
return studentFlux.collectList().map(list -> {
school.setStudentList(list);
return school;
});
});
}
【问题讨论】:
标签: reactive-programming spring-webflux