我正在尝试从另一个微服务中获取数据。假设您有三种微服务:州,学校和学生。您可以从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
更新
解决方案
非常感谢@ 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;
});
});
}
答案 0 :(得分:0)
编辑:第二次尝试。因此,我看不到什么特别的东西。使用collectList
方法并在map
函数中分配它。 map
函数返回范围内的学校对象。我必须进行一些调试,以确保我的类正确支持序列化/反序列化。
WebClient.create().get().uri(URI.create("http://localhost:8082/ss/school?state=CA"))
.accept(MediaType.APPLICATION_JSON)
.exchange()
.flatMapMany(cr->cr.bodyToFlux(School.class))
.flatMap(school->{
return WebClient.create().get().uri(URI.create("http://localhost:8081/ss/student?school="+school.getName()))
.accept(MediaType.APPLICATION_JSON)
.exchange()
.flatMapMany(crs->crs.bodyToFlux(Student.class))
.collectList()
.map(sl->{
school.setStudents(sl);
return school;
});
})
.subscribe(System.out::println);