当我不使用 webflux 时,我的代码
1.public Map<String, Object> registerService(User user) {
Map<String, Object> map = new HashMap<>(8);
map.put("status":1);
userRepository.save(user);
return map;
当我使用 webflux 时,我的代码
2.public Mono<Map<String, Object>> registerService(User user) {
Map<String, Object> map = new HashMap<>(8);
map.put("status", 1);
userRepository.save(user);
return Mono.just(map);
}
并且2.响应是“{”status”:1}”,但是用户不插入mongo
在 webflux 中,我无法得到与 1. 相同的响应,那么该怎么做?
我不想返回用户,只返回我自定义的 map ,然后用户插入mongodb
最佳答案
假设您的用户存储库返回类型也是 Mono,并且您想返回自定义 map ,您可以这样做
public Mono<Map<String, Object>> registerService(User user) {
return userRepository.save(user)
.map(__ -> {
Map<String, Object> map = new HashMap<>();
map.put("status", 1);
return map;
});
}
在上面的代码中,您没有破坏 react 链,而是按照您想要的方式返回自定义 map 。
关于java - spring5 webflux,如何返回自定义json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52821060/