How to check if Mono is empty?

后端 未结 3 1069
南旧
南旧 2021-01-31 16:11

I\'m developing a app with Spring Boot 2.0 and Kotlin using the WebFlux framework.

I want to check if a user id exits before save a transaction. I\'m stucked in a simple

3条回答
  •  清酒与你
    2021-01-31 16:43

    We can use switchIfEmpty method for this

    Below example, I'm checking if the user exists with email if not then add it

    userRepository.findByEmail(user.getEmail())
                    .switchIfEmpty(s -> {
                        user.setStatus("InActive");
                        String encodedPassword = DigestUtils.sha256Hex(user.getPassword());
                        user.setPassword(encodedPassword);
                        userRepository.save(user).subscribe();
                        s.onComplete();
                    }).then(Mono.just(user));
    

提交回复
热议问题