我有以下请求处理程序fun x(req: ServerRequest) = req.toMono().flatMap {...val oldest = myRepository.findOldest(...) // this is the object I want to modify...val v= anotherMongoReactiveRepository.save(Y...
![](https://oss.womengda.net/imgfile/2310/1ER05WX0b0-5C12.jpg)
我有以下请求处理程序
fun x(req: ServerRequest) = req.toMono()
.flatMap {
...
val oldest = myRepository.findOldest(...) // this is the object I want to modify
...
val v= anotherMongoReactiveRepository.save(Y(...)) // this saves successfully
myRepository.save(oldest.copy(
remaining = (oldest.remaining - 1)
)) // this is not saved
ok().body(...)
}
和以下mongodb反应库
@Repository
interface MyRepository : ReactiveMongoRepository<X, String>, ... {
}
问题是执行save()方法后,对象中没有更改.我设法用save().block()修复了这个问题但是我不知道为什么第一个保存在另一个存储库上工作而这个没有.为什么需要这个块()?
解决方法:
在有人订阅被动发布者之前,没有任何事情发生.这就是你使用block()时它开始工作的原因.如果你需要调用DB并在另一个DB请求中使用结果,而不是使用mono / Flux运算符(如map(),flatMap(),…)来构建所需操作的管道,然后返回结果Mono / Flux作为控制器的响应. Spring将订阅Mono / Flux并返回请求.你不需要阻止它.并且不建议这样做(使用block()方法).
简单示例如何在Java中使用MongoDB反应式存储库:
@GetMapping("/users")
public Mono<User> getPopulation() {
return userRepository.findOldest()
.flatMap(user -> { // process the response from DB
user.setTheOldest(true);
return userRepository.save(user);
})
.map(user -> {...}); // another processing
}
本文标题为:java – 用spring数据更新对象mongodb和kotlin不能正常工作
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 关于jsp中cookie丢失问题(详解) 2023-07-31
- Java去掉小数点后面无效0的方案与建议 2023-02-19
- Java实现简单的聊天室功能 2022-12-16
- java内存模型的理解 volatile理解 2023-09-01
- JSP状态管理的简单介绍 2023-07-31
- SpringMVC下获取验证码实例详解 2023-07-30
- JSP实现分页效果 2023-07-30
- 一文探索Java文件读写更高效方式 2023-02-20
- 详解MyBatis中column属性的总结 2023-05-19
- Mybatis条件if test如何使用枚举值 2022-12-07