[英]How to return null if the Mono is empty
I am calling an external service to get externalId
, in case of that service doesn't work, all what i need is just populate the value with null
我正在调用外部服务来获取externalId
,以防该服务不起作用,我所需要的只是用null
填充值
return externalClient.getExternalId() //returns Mono<String> or Mono.empty()
.map(id -> {
//in case of empty stream, i need call entity.setExternalId(null);
entity.setExternalId(id);
return entity;
});
Try the following:尝试以下操作:
return externalClient.getExternalId()
.map(id -> {
entity.setExternalId(id);
return entity;
})
.switchIfEmpty(() -> {
entity.setExternalId(null);
return Mono.just(entity);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.