title: How to properly send Mono<ResponseEntity> as JSON in Netty Reactor HTTP Server response
body: <p>I am trying to figure out how to properly send response with ResponseEntity as JSON from Netty Reactor HTTP Server.</p>
  
  <p>My current implementation reacts on request from WebClient and should send back response with some ResponseEntity status (let's assume just HTTP OK). </p>
  
  <p>Unfortunately I'm still getting InvalidDefinitionException on the client side saying that it not possible to construct instance due to no default constructor. </p>
  
  <p>I know what it means but for example Spring Webflux can have return type of rest endpoint  Mono as well and no issues on client side will appear.
  So is it somehow possible to properly serialize entity as JSON on server side and deserialize it at the client side?</p>
  
  <p>This is my client</p>
  
  <pre><code>import org.springframework.web.reactive.function.client.WebClient;
  
  public Mono&lt;ResponseEntity&gt; postRequest(final Object body, final String uri) {
          return webClient.post()
                  .uri(uri)
                  .contentType(MediaType.APPLICATION_JSON)
                  .body(BodyInserters.fromValue(body))
                  .exchange()
                  .flatMap(clientResponse -&gt; clientResponse.toEntity(ResponseEntity.class));
      }
  </code></pre>
  
  <p>This is my Server</p>
  
  <pre><code>    public void runWithPost(final String endpointPath, final ServerCallback callback) {
          server = HttpServer.create()
                  .host(this.host)
                  .port(this.port)
                  .route(routes -&gt;
                          routes.post(endpointPath, (request, response) -&gt;
                                  response.addHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                                          .sendString(Mono.just(getJSON(callback.handleCallback())))))
                  .wiretap(true)
                  .bindNow();
  
          System.out.println("Starting server...");
      }
  
      private String getJSON(final ResponseEntity responseEntity) {
          String json = StringUtils.EMPTY;
          try {
              json = objectMapper.writeValueAsString(responseEntity);
              System.out.println("Serialized JSON: " + json);
          } catch (final JsonProcessingException ex) {
              System.err.println("JSON serializer error: " + ex.getMessage());
          }
  
          return json;
      }
  </code></pre>
  
  <p>This is callback</p>
  
  <pre><code>public interface ServerCallback {
  
      ResponseEntity handleCallback();
  
  }
  </code></pre>
  
  <p>and usage</p>
  
  <pre><code>reactiveRestServer.runWithPost("/transaction", () -&gt; ResponseEntity.ok().build());
  </code></pre>
  
  <p>Unfortunately on the client side I do not get HTTP status OK but deserialization exception:</p>
  
  <pre><code>2020-04-28 16:09:35.345 ERROR 15136 --- [ctor-http-nio-2] c.a.t.t.inbound.ArpMessageServiceImpl    : Type definition error: [simple type, class org.springframework.http.ResponseEntity]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.http.ResponseEntity` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
   at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 2]
  2020-04-28 16:09:35.349  WARN 15136 --- [ctor-http-nio-2] io.netty.util.ReferenceCountUtil         : Failed to release a message: DefaultLastHttpContent(data: PooledSlicedByteBuf(freed), decoderResult: success)
  
  io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
      at io.netty.util.internal.ReferenceCountUpdater.toLiveRealRefCnt(ReferenceCountUpdater.java:74) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
  </code></pre>
  
  <p>What I am missing?</p>