WebFlux functional: How to detect an empty Flux and return 404?

前端 未结 4 1538
灰色年华
灰色年华 2020-12-30 10:03

I\'m having the following simplified handler function (Spring WebFlux and the functional API using Kotlin). However, I need a hint how to detect an empty Flux and then use n

相关标签:
4条回答
  • 2020-12-30 10:42

    Use Flux.hasElements() : Mono<Boolean> function:

    return customerFlux.hasElements()
                       .flatMap {
                         if (it) ok().body(customerFlux)
                         else noContent().build()
                       }
    
    0 讨论(0)
  • 2020-12-30 10:46

    From a Mono:

    return customerMono
               .flatMap(c -> ok().body(BodyInserters.fromObject(c)))
               .switchIfEmpty(notFound().build());
    

    From a Flux:

    return customerFlux
               .collectList()
               .flatMap(l -> {
                   if(l.isEmpty()) {
                     return notFound().build();
    
                   }
                   else {
                     return ok().body(BodyInserters.fromObject(l)));
                   }
               });
    

    Note that collectList buffers data in memory, so this might not be the best choice for big lists. There might be a better way to solve this.

    0 讨论(0)
  • 2020-12-30 10:55

    In addition to the solution of Brian, if you are not want to do an empty check of the list all the time, you could create a extension function:

    fun <R> Flux<R>.collectListOrEmpty(): Mono<List<R>> = this.collectList().flatMap {
            val result = if (it.isEmpty()) {
                Mono.empty()
            } else {
                Mono.just(it)
            }
            result
        }
    

    And call it like you do it for the Mono:

    return customerFlux().collectListOrEmpty()
                         .switchIfEmpty(notFound().build())
                         .flatMap(c -> ok().body(BodyInserters.fromObject(c)))
    
    0 讨论(0)
  • 2020-12-30 10:59

    I'm not sure why no one is talking about using the hasElements() function of Flux.java which would return a Mono.

    0 讨论(0)
提交回复
热议问题