reactive-programming - 如何链接来自不同服务的响应,以便在 WebFlux 中使用“那些响应”创建 Flux 响应?
问题描述
关于问题的语境化:
我正在尝试链接来自多个服务的数据,以便聚合/合并它们的响应;
我的目标是使用从“合并响应”创建的对象列表创建最终通量。
合并基于2个服务(userService + postService)
- 以下是上述情况的代码:
代码:
@Slf4j
@Service
@AllArgsConstructor
public class BlogService implements BlogServiceInt {
private final UserServiceInt userService;
private final PostServiceInt postService;
private final CommentServiceInt commentService;
private final ModelMapper conv;
@Override
public Flux<UserAllDto> findAllShowAllDto() {
return userService
.findAll()
.flatMap(user -> Flux.fromIterable(List.of(conv.map(user,UserAllDto.class))))
.flatMap(userAllDto -> {
postService
.findPostsByAuthorId(userAllDto.getId())
.map(post -> conv.map(post,PostAllDto.class))
.collectList()
.flatMap(list -> {
if (!list.isEmpty()) userAllDto.setPosts(list);
return Mono.just(userAllDto);
});
return Flux.fromIterable(List.of(userAllDto));
}
);
}
}
问题:
- 如何完成“userAllDto.setPosts”,
- 通过获取“PostAllDto”对象创建一个列表,
- 并将此列表插入“userAllDto.setPosts”?
- 通过获取“PostAllDto”对象创建一个列表,
当前有问题的 JsonResponse(邮递员):
[
{
"id": "60b0306f275ea3018b167dde",
"name": "p",
"posts": []
},
{
"id": "60b03070275ea3018b167ddf",
"name": "p",
"posts": [
{
"id": null,
"title": null,
"listComments": []
}
]
}
]
更新:
找到解决方案
@Override
public Flux<UserAllDto> findAllShowAllDto() {
return userRepo
.findAll()
.flatMap(user -> {
UserAllDto userDto = mapper.map(user,UserAllDto.class);
final Mono<UserAllDto> userAllDtoMono =
postService
.findPostsByAuthorId(userDto.getId())
.flatMap(post -> {
PostAllDto postDto = mapper.map(post,PostAllDto.class);
final Mono<PostAllDto> postAllDtoMono =
commentService.findCommentsByPostId(postDto.getPostId())
.map(c -> mapper.map(c,CommentAllDto.class))
.collectList()
.flatMap(list -> {
postDto.setListComments(list);
return Mono.just(postDto);});
return postAllDtoMono.flux();})
.collectList()
.flatMap(list -> {
userDto.setPosts(list);
return Mono.just(userDto);
});
return userAllDtoMono.flux();
});
}
解决方案的 JsonResponse(邮递员):
[
{
"id": "60b9284e08a653638c22bd97",
"name": "bbbbbbbb ",
"posts": [
{
"postId": "60b929a808a653638c22bd9d",
"title": "bbbbbbbbbb111111111111",
"idAuthor": "60b9284e08a653638c22bd97",
"listComments": [
{
"commentId": "60b92bad08a653638c22bd9e",
"postId": "60b929a808a653638c22bd9d",
"idAuthor": "60b9292e08a653638c22bd9b",
"text": "ccccccccccccccccc 2222"
},
{
"commentId": "60b92c1708a653638c22bd9f",
"postId": "60b929a808a653638c22bd9d",
"idAuthor": "60b9285908a653638c22bd98",
"text": "aaaaaaaaaaaaaaaaaaaa 2222"
}
]
}
]
},
{
"id": "60b9285908a653638c22bd98",
"name": "aaaaaaa ",
"posts": [
{
"postId": "60b9287808a653638c22bd99",
"title": "aaaaaaa1111111111111",
"idAuthor": "60b9285908a653638c22bd98",
"listComments": [
{
"commentId": "60b928f408a653638c22bd9a",
"postId": "60b9287808a653638c22bd99",
"idAuthor": "60b9284e08a653638c22bd97",
"text": "bbbbbbbbbbbbb 1111"
},
{
"commentId": "60b9294a08a653638c22bd9c",
"postId": "60b9287808a653638c22bd99",
"idAuthor": "60b9292e08a653638c22bd9b",
"text": "ccccccccccccccccc 1111"
}
]
}
]
}
]
非常感谢您的帮助
解决方案
推荐阅读
- javascript - 访问 Promise.then() 中的拖放对象?
- python - 每次代码运行时如何保存数据并附加到它
- ios - 将数据从 UIViewController 传递到 UITabBarController 始终为零
- flutter - 是否可以在飞镖中动态设置方法的返回类型
- python - 在 Android Studio 中使用 Chaquopy 将值插入到 python 文件中
- vba - 从抓取的表格中提取单元格值的最有效方法
- c# - 是否可以在 HwndHost 中托管 WPF 页面?
- chart.js - 如何在工具提示回调afterBody中将逗号分隔的字符串分成新行?
- stata - 在stata矩阵中生成新列
- python - I am trying to call information on venues in a 500 m radius of a location via the foursquare API. I keep getting getting the error: [WinError 10061]