spring-webflux - Spring boot webFlux 内部单声道不发出数据

标签 spring-webflux project-reactor

我是 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 实例,您需要将其重新分配给变量,否则管道中不会考虑该运算符。

查看this tip in the project reactor documentation .

关于spring-webflux - Spring boot webFlux 内部单声道不发出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041844/

相关文章:

java - Spring react : convert Flux<Wrapper<X>> to Flux<X>

spring-boot - 响应式 Spring WebClient - 进行 SOAP 调用

java - 使用 Spring Webflux 的 HTTP 201 的绝对位置 header

java - 将 Mono 的列表转换为 Flux

java - 在 Reactor 应用程序中测试对 Flux 的背压应用

java - Project Reactor 条件执行

java - 将一个单声道对象转换为两个不同的单声道对象,并且根单声道对象执行两次

spring-boot - Spring Cloud Sleuth如何开箱即用地在Webflux中传播MDC上下文,以便其内容可以记录在不同的线程中?

java - Spring WebFlux : Serve files from controller

Java Flux GroupedFlux count() 打印