title: Jersey response.readEntity(...) sometimes returns null
body: <p>Consider this snippet from my REST client (Jersey 2.26). It's used to post objects and return the response object. If the REST server returns an error (status >= 400), then instead of returning an entity of type <code>T</code> I want to read an entity of type <code>ErrorMessage</code> and throw an exception containing the error message object.</p>
  
  <pre><code>protected &lt;T&gt; T post(final Class&lt;T&gt; type,
                       final Object entity,
                       final Map&lt;String, Object&gt; queryParams,
                       final String methodPath,
                       final Object... arguments) {
    return postResponse(
      getInvocationBuilderJson(methodPath,
                               queryParams,
                               arguments),
      entity
    ).readEntity(type);
  }
  
  protected Response postResponse(final Invocation.Builder invocationBuilder,
                                  final Object entity) {
    return handleErrors(
      invocationBuilder.post(Entity.entity(entity,
                                           MediaType.APPLICATION_JSON_TYPE))
    );
  }
  
  protected Response handleErrors(final Response response) {
    if (response.getStatus() &gt;= 400) {
      throw new InvocationException(response.readEntity(ErrorMessage.class));
    }
    return response;
  }
  </code></pre>
  
  <p>If no error occurs (status &lt; 400), then my object of type <code>T</code> is returned as expected. However, when an error does occur, <code>response.readEntity(ErrorMessage.class)</code> returns <code>null</code>. But (and this is the strange part), this <strong>does</strong> get me data (at the <code>handleErrors</code> method):</p>
  
  <pre><code>byte[] data = readAllBytes((InputStream) response.getEntity());
  </code></pre>
  
  <p>I could use that and deserialize it manually.. but I would first like to know if there are any options to fix this without implementing workarounds.</p>