带有null的Mono.zip

带有null的Mono.zip,mono,reactive-programming,spring-webflux,Mono,Reactive Programming,Spring Webflux,我的代码: Mono.zip( credentialService.getCredentials(connect.getACredentialsId()), credentialService.getCredentials(connect.getBCredentialsId()) ) .flatMap(... 从前端,我们可以获得带有两个字段的connect对象: connect{ aCredentialsId : UUID //required

我的代码:

Mono.zip(
            credentialService.getCredentials(connect.getACredentialsId()),
            credentialService.getCredentials(connect.getBCredentialsId())
)
.flatMap(...
从前端,我们可以获得带有两个字段的
connect
对象:

connect{
aCredentialsId : UUID //required
bCredentialsId : UUID //optional
}
因此,有时第二行
credentialService.getCredentials(connect.getBCredentialsId())
可以返回
Mono.empty

当我的第二个字段
bCredentialsId
为空时,如何编写为这个空单声道准备的代码


我该怎么办?如果值为空
则返回Mono.just(新对象)
,然后检查
obj.getValue!=空值
???我需要从DB中获取两个不同值的数据

这里我更喜欢的策略是声明一个
可选()
实用方法,如下所示:

public class Utils {

    public static <T> Mono<Optional<T>> optional(Mono<T> in) {
        return in.map(Optional::of).switchIfEmpty(Mono.just(Optional.empty()));
    }

}

(…假设您有一个
Connect
对象,该对象当然将
可选的
作为第二个参数。)

更简单的方法是使用mono的
defaultIfEmpty
方法

Mono<String> m1 = credentialService.getCredentials(connect.getACredentialsId());
Mono<String> m2 = credentialService.getCredentials(connect.getBCredentialsId()).defaultIfEmpty("");

Mono.zip(m1, m2).map(t -> connectService.connect(t.getT1(), t.getT2()));
monom1=credentialService.getCredentials(connect.getACredentialsId());
Mono m2=credentialService.getCredentials(connect.getBCredentialsId()).defaultIfEmpty(“”);
Mono.zip(m1,m2).map(t->connectService.connect(t.getT1(),t.getT2());

说明:如果m2为null,则将空字符串作为默认值而不是null。

这是否回答了您的问题?
Mono<String> m1 = credentialService.getCredentials(connect.getACredentialsId());
Mono<String> m2 = credentialService.getCredentials(connect.getBCredentialsId()).defaultIfEmpty("");

Mono.zip(m1, m2).map(t -> connectService.connect(t.getT1(), t.getT2()));