助焊剂。正确使用

时间:2019-08-15 18:21:37

标签: spring-webflux project-reactor

我有一个Flux.fromIterable(ID列表)。我想找出任何记录是否为null。因此我尝试使用Flux.any,但是我发现我什至没有打印任何内容,而是直接转到doOnEach,因为结果输出为false。我们该如何解决? 该解决方案不应局限于空检查,它可以是任何布尔条件。


    Mono<Boolean> isAnyNull =Flux.fromIterable(request.getIds())
        .switchIfEmpty(Mono.error(new SomeException("No elements")))
        .flatMap(id->{
          return FooRepo.find(id);
          }).any(p->{
          System.out.println("check is any null p?."+p);
          return ((p==null)||(p.getId()==null));
        }).doOnEach(System.out::println);

我在下面尝试了一个临时的怪异修复程序,但我不确定这是否正确。我也认为这仅适用于null。

Mono<Boolean> isAnyNull = Flux.fromIterable(request.getds())
        .switchIfEmpty(Mono.error(new SomeException("No elements")))
        .flatMap(id -> {
          return FooRepo.find(id);
         })
        .switchIfEmpty(Mono.error(new SomeException("INVALID_ID"))).hasElements()
        .doOnEach(System.out::println);

更新:这也可以使用流检查是否为空-感谢改进的评论

Flux.fromIterable(request.getIds())
     .switchIfEmpty(Mono.error(new SomeException("No elements")))
        .flatMap(id -> {
          return FooRepo.find(id);
        })
        .any(foo -> {
         return (Objects.nonNull(foo)||foo.getId()!=null);
        }).doOnEach(System.out::println);

0 个答案:

没有答案