Rajeev Ranjan
Rajeev Ranjan

Reputation: 19

Spring boot webFlux inner mono not emitting data

I m new in spring boot webflux . I am facing a little problem here "userMono is not empty" but this part of the code is executing "switchIfEmpty(Mono.just("hello123"))"

Here is my code

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

Upvotes: 2

Views: 867

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59231

He problem is you’re not chaining operators in your implementation. Each operator call returns a new Publisher instance and you need to reassign it to a variable otherwise this operator won’t be taken into account in the pipeline.

Check out this tip in the project reactor documentation.

Upvotes: 2

Related Questions