spring - 如何使用每个 Flux 元素获得的 Mono 的值过滤 Flux?
问题描述
假设我有一个Flux<User>
和另一个Mono<Department>
。Department
有和 id 和部门名称。
我有一项服务可以返回用户所属的部门:
departmentService.getByUserId(userId: Int) : Mono<Department>
我想过滤不在部门中的用户(departmentService.getByUserId(userId: Int)
返回一个空的 Mono)或在部门名称不是 HR 的部门中的用户。如何才能做到这一点?
users.flatMap { user ->
departMentService.getByUserId(user.id)
...
}
解决方案
我的解决方案包括使用 Pair 来传播我在过滤器中需要的信息的聚合:
users.flatMap { user ->
departMentService.getByUserId(user.id)
.map { Pair(user, it) }
}.filter { it.second == null || it.second.departmentName != "HR" }
推荐阅读
- javascript - 同时使用带有 href 和 onClick 的按钮
- python - 如何使用字典或数组python中的参数调用类方法
- json - 放置数据库数据但使用手动输入时不显示材质 UI 自定义树
- r - R运行分类跨框架实验中的Vtreat问题
- javascript - 在类似计划的滚动视图组件中水平显示 FlatList
- python-3.x - python通过文本在对象属性中获取对象
- junit5 - 无法运行测试类“没有使用测试运行器 'Junit5' 找到测试”
- php - CDN 链接无法使用 PHP 包含
- reactjs - 如何严格键入包含要渲染的内部组件和内部组件道具的组件道具?
- kubernetes - kubernetes 入口服务不会在 http 或 https 中转发