【问题标题】:Spring boot webFlux inner mono not emitting dataSpring Boot webFlux内部单声道不发射数据
【发布时间】:2018-12-05 02:43:32
【问题描述】:

我是 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");
    });

【问题讨论】:

    标签: spring-webflux project-reactor


    【解决方案1】:

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

    查看this tip in the project reactor documentation。

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2020-08-12
      • 2019-10-06
      • 2018-09-24
      • 2019-11-08
      • 2018-11-08
      • 2018-08-24
      • 2020-10-22
      • 1970-01-01
      相关资源
      最近更新 更多