如何结合 Mono 和 Flux 作为参数来创建新的 Mono?
How to combine Mono and Flux as parameters to create new Mono?
我的 REST 控制器方法应该 return Mono,它必须由对另一个 Web 服务的 2 个并行请求构建并处理它们的响应,其中一个请求 return Mono 和另一个请求 return Flux
如何结合Mono和Flux的响应并进行处理?
型号:
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ClassModel {
@Id
private String id;
private String roomNr;
private String className;
private String teacherId;
private List<String> studentIds;
public void addStudentId(String studentId) {
studentIds.add(studentId);
}
}
控制器:
public Mono<ClassModel> addRandomClassFull() {
return Mono.zip(
//request teacher microservice and return Mono - single teacher
reactiveNetClient.addRandomTeacher(),
//request students microservice and return Flux - list of students
reactiveNetClient.addRandomStudents(10),
(teacher, students) -> {
ClassModel classModel = new ClassModel();
classModel.setRoomNr("24B");
classModel.setClassName(faker.educator().course());
classModel.setTeacherId(teacher.getId());
students.forEach(student -> classModel.addStudentId(student.getId());
return classModel;
}).flatMap(classRepository::save);
}
显然,控制器是错误的:
1) Mono.zip()
需要 2 个或更多 Mono,我有 Mono 和 Flux - 如何组合它们?
2) 也不确定:
students.forEach(student -> classModel.addStudentId(student.getId());
是否正确?
有什么建议吗?
- 您可以将方法
addRandomStudents()
更改为 return Mono<List<Student>>
您可以在 Flux<Student>
上使用 collectList()
,它将 return 然后 Mono<List<Student>>
然后在 addStudents()
将转换 Student
反对 id。
public Mono<ClassModel> addRandomClassFull() {
return Mono.zip(
reactiveNetClient.addRandomTeacher(),
reactiveNetClient.addRandomStudents(10).collectList(),
(teacher, students) -> {
ClassModel classModel = new ClassModel();
classModel.setRoomNr("24B");
classModel.setClassName(faker.educator().course());
classModel.setTeacherId(teacher.getId());
classModel.addStudents(students);
return classModel;
}).flatMap(classRepository::save);
}
我的 REST 控制器方法应该 return Mono,它必须由对另一个 Web 服务的 2 个并行请求构建并处理它们的响应,其中一个请求 return Mono 和另一个请求 return Flux
如何结合Mono和Flux的响应并进行处理?
型号:
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ClassModel {
@Id
private String id;
private String roomNr;
private String className;
private String teacherId;
private List<String> studentIds;
public void addStudentId(String studentId) {
studentIds.add(studentId);
}
}
控制器:
public Mono<ClassModel> addRandomClassFull() {
return Mono.zip(
//request teacher microservice and return Mono - single teacher
reactiveNetClient.addRandomTeacher(),
//request students microservice and return Flux - list of students
reactiveNetClient.addRandomStudents(10),
(teacher, students) -> {
ClassModel classModel = new ClassModel();
classModel.setRoomNr("24B");
classModel.setClassName(faker.educator().course());
classModel.setTeacherId(teacher.getId());
students.forEach(student -> classModel.addStudentId(student.getId());
return classModel;
}).flatMap(classRepository::save);
}
显然,控制器是错误的:
1) Mono.zip()
需要 2 个或更多 Mono,我有 Mono 和 Flux - 如何组合它们?
2) 也不确定:students.forEach(student -> classModel.addStudentId(student.getId());
是否正确?
有什么建议吗?
- 您可以将方法
addRandomStudents()
更改为 returnMono<List<Student>>
您可以在
Flux<Student>
上使用collectList()
,它将 return 然后Mono<List<Student>>
然后在addStudents()
将转换Student
反对 id。public Mono<ClassModel> addRandomClassFull() { return Mono.zip( reactiveNetClient.addRandomTeacher(), reactiveNetClient.addRandomStudents(10).collectList(), (teacher, students) -> { ClassModel classModel = new ClassModel(); classModel.setRoomNr("24B"); classModel.setClassName(faker.educator().course()); classModel.setTeacherId(teacher.getId()); classModel.addStudents(students); return classModel; }).flatMap(classRepository::save); }