首页 文章

Spring引导webFlux内部单声道不发射数据

提问于
浏览
0

我是 Spring 季启动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");
    });

1 回答

  • 0

    他的问题是你在实现中没有链接运算符 . 每个操作员调用都返回一个新的Publisher实例,您需要将其重新分配给变量,否则将不会在管道中考虑此操作符 .

    看看this tip in the project reactor documentation .

相关问题