来自
Mono:
return customerMono.flatMap(c -> ok().body(BodyInserters.fromObject(c))).switchIfEmpty(notFound().build());
来自
Flux:
return customerFlux.collectList().flatMap(l -> { if(l.isEmpty()) { return notFound().build(); } else { return ok().body(BodyInserters.fromObject(l))); }});请注意,
collectList这会在内存中缓冲数据,因此对于大列表而言,这可能不是最佳选择。可能有更好的方法来解决此问题。



