title: Spring webflux "Only one connection receive subscriber allowed" if return server response from switchIfEmpty
body: <p>I would like to put a case where if object exist then send error if not then create new user.</p>
  
  <p>here is my handler:</p>
  
  <pre><code>public Mono&lt;ServerResponse&gt; createUser(ServerRequest request) {
          Mono&lt;UserBO&gt; userBOMono = request.bodyToMono(UserBO.class);
          Mono&lt;String&gt; email = userBOMono.map(UserBO::getEmail);
          Mono&lt;User&gt; userMono = email.flatMap(userRepository::findByEmail);
          return userMono.flatMap(user -&gt; {
              Mono&lt;ErrorResponse&gt; errorResponseMono = errorHanlder.handleEmailAlreadyExist();
              return ServerResponse.status(HttpStatus.CONFLICT)
                      .contentType(MediaType.APPLICATION_JSON)
                      .body(errorResponseMono, ErrorResponse.class);
          }).switchIfEmpty(Mono.defer(() -&gt; {
              Mono&lt;User&gt; newUserMono = userBOMono.flatMap(userMapping::mapUserBOToUser);
              Mono&lt;User&gt; dbUserMono = newUserMono.flatMap(userRepository::save);
              return ServerResponse.status(HttpStatus.CREATED)
                      .contentType(MediaType.APPLICATION_JSON)
                      .body(dbUserMono, User.class);
  
          }));
  </code></pre>
  
  <p>if Mono is not empty then its return conflict that what I want if if empty then create new but its throwing below error:</p>
  
  <pre><code>java.lang.IllegalStateException: Only one connection receive subscriber allowed.
      at reactor.ipc.netty.channel.FluxReceive.startReceiver(FluxReceive.java:276) ~[reactor-netty-0.7.8.RELEASE.jar:0.7.8.RELEASE]
      at reactor.ipc.netty.channel.FluxReceive.lambda$subscribe$2(FluxReceive.java:127) ~[reactor-netty-0.7.8.RELEASE.jar:0.7.8.RELEASE]
      at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163) ~[netty-common-4.1.27.Final.jar:4.1.27.Final]
      at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) ~[netty-common-4.1.27.Final.jar:4.1.27.Final]
      at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.27.Final.jar:4.1.27.Final]
      at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:464) ~[netty-transport-4.1.27.Final.jar:4.1.27.Final]
      at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-common-4.1.27.Final.jar:4.1.27.Final]
      at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_131]
  </code></pre>
  
  <p>Update Note: its correct behavior as per method definition:</p>
  
  <pre><code>switchIfEmpty(Mono&lt;? extends T&gt; alternate)
  Fallback to an alternative Mono if this mono is completed without data
  </code></pre>
  
  <p>Means when I am sending empty Mono in body its work fine:</p>
  
  <pre><code>return ServerResponse.status(HttpStatus.CREATED)
                      .contentType(MediaType.APPLICATION_JSON)
                      .body(Mono.empty(), User.class);
  </code></pre>
  
  <p>so what is solution to handle swtichIfEmpty case if I would like to send Mono object as return from it.</p>