title: How to return a Mono<ResponseEntity> where the response entity can be of two different types
body: <p>I am new to Spring Webflux / Reactor Core and am trying to perform the following functionality:</p>
  
  <ol>
  <li><p>call userservice.LoginWebApp()</p></li>
  <li><p>If a user is returned, return ResponseEntity of type "User". If empty, Return ResponseEntity of type "String"</p></li>
  </ol>
  
  <p>The following code gives a type error as .defaultIfEmpty() expects ResponseEntity of type user. Can you please advise on the correct operator / method to implement this functionality. </p>
  
  <pre><code>@PostMapping("api/user/login/webApp")
  public Mono&lt;ResponseEntity&gt; login(@RequestBody Credentials credentials, ServerWebExchange serverWebExchange) {
       return userService.loginWebApp(credentials, serverWebExchange)
               .map(user -&gt; ResponseEntity.status(HttpStatus.OK).body(user))
               .defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password"));
  }
  </code></pre>