title: Return 404 when a Flux is empty
body: <p>I am trying to return a 404 when a Flux is empty, similar to here:<a href="https://stackoverflow.com/questions/45903813/webflux-functional-how-to-detect-an-empty-flux-and-return-404?rq=1">WebFlux functional: How to detect an empty Flux and return 404?</a></p>
  
  <p>My main concern is that, when you check if the flux has elements it emmits that value and you loose it. And when I try to use switch if empty on the Server Response it is never called (I secretly think it is because the Mono is not empty, only the body is empty).</p>
  
  <p>Some code of what I am doing (I do have a filter on my Router class checking for DataNotFoundException to return a notFound):</p>
  
  <pre><code>Flux&lt;Location&gt; response = this.locationService.searchLocations(searchFields, pageToken);
  return ok()
          .contentType(APPLICATION_STREAM_JSON)
          .body(response, Location.class)
          .switchIfEmpty(Mono.error(new DataNotFoundException("The data you seek is not here.")));
  </code></pre>
  
  <p>^This never calls switchIfEmpty</p>
  
  <pre><code>Flux&lt;Location&gt; response = this.locationService.searchLocations(searchFields, pageToken);
  
  return response.hasElements().flatMap(l -&gt;{
     if(l){
         return ok()
                 .contentType(APPLICATION_STREAM_JSON)
                 .body(response, Location.class);
     } 
     else{
         return Mono.error(new DataNotFoundException("The data you seek is not here."));
     }
  });
  </code></pre>
  
  <p>^This looses the emitted element on hasElements.</p>
  
  <p>Is there a way to either recover the emitted element in hasElements or to make the switchIfEmpty only check the contents of the body?</p>