title: How to return Response 401 in Jersey with json content
body: <p>I am trying to return UNAUTHORIZED response with json content in one of my REST method. I am using Jersey 2.5.1 with Spring.</p>
  
  <p>I am using following annotations on my method:</p>
  
  <pre><code>@Path("login")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response myMethod(@Valid CmdObject cmd) {
    ...
    return Response.status(Response.Status.UNAUTHORIZED).entity(AUTHENTICATION_ERROR_MSG).build();
  }
  </code></pre>
  
  <p>In case if AUTHENTICATION_ERROR_MSG is just a String for example "Authentication problem" everything is fine and a response is returned however it is String content only.</p>
  
  <p>In case if I replace AUTHENTICATION_ERROR_MSG with some Object then 500 Http error is returned. </p>
  
  <p>Any suggestion what may be wrong?</p>