我是 Spring Boot webflux 的新手。我在这里面临一个小问题“userMono 不为空”,但这部分代码正在执行“switchIfEmpty(Mono.just("hello123"))"
这是我的代码
Mono<String> someMono = serverRequest.bodyToMono(String.class);
Mono<List<String>> listMsgMono = someMono.flatMap(json -> {
JSONObject jsonObject = Util.getJsonObjectFromString(jsonString);
String id = Util.getStringFromJSON(jsonObject, "id");
JSONArray jsonArray = Util.getJSONArrayFromJSON(jsonObject, "array");
int length = jsonArray.length();
for (int index = 0; index < length; index++) {
String email = Util.getStringFromJSONArray(jsonArray, index);
LOGGER.debug("email :" + email);
Mono<User> userMono = repository.findByEmail(email);
//inner mono is not emmitting data
userMono.flatMap(user -> {
otherRepository.findById(id).flatMap(l -> {
return Mono.just("all welcome");
}).switchIfEmpty(someRepository
.findById(id).flatMap(r ->{
return Mono.just("all done");
}).switchIfEmpty((Mono.just("hello0000"));
return Mono.just("successfull");
})
.switchIfEmpty(Mono.just("hello123"));
}
return Mono.just("hello");
});
最佳答案
问题是你没有在实现中链接运算符。每个运算符调用都会返回一个新的 Publisher 实例,您需要将其重新分配给变量,否则管道中不会考虑该运算符。
关于spring-webflux - Spring boot webFlux 内部单声道不发出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041844/