Skip to main content
 首页 » 编程设计

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

2025年05月04日41bhlsheji

我是 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 .