title: Can't get JSON from http request
body: <p>I implemented a RESTful web-service based on Jersey.
  When sending my request I first check if some mandatory parameters are defined and if not I return a Response with an error code and an error message.
  Here is the snippet:</p>
  
  <pre><code>@Path( "/groups" )
  @RequestScoped
  @Consumes( MediaType.APPLICATION_JSON )
  @Produces( value = {MediaType.APPLICATION_JSON, MediaType.TEXT_XML} )
  public class GroupResource
  {
    ...
    @POST
    public Response createGroup( Group group, @Context UriInfo uriInfo )
    {
      logger.info("-------------------");
      logger.info("Create group");
      logger.fine(group.toString());
      logger.info("-------------------");
  
      // check mandatory fields
      if (!checkMandatoryFields(group, errorMessages))
      {
        return Response.status(Status.BAD_REQUEST).entity(errorMessages).build();
      }
    ...
  }
  </code></pre>
  
  <p>I then implemented a JUnit test to test it:</p>
  
  <pre><code>@Test
    public void testCreateGroup()
    {
      try
      {
        URL url = new URL(URL_GROUPS_WS);
  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
  
        String json2send = "{\"grid\":\"1\", \"gidNumber\":\"2\", \"groupName\":\"TestGroup\", \"groupDescription\":\"Initial description\", \"targetSystems\":[\"ADD TS1\"]}";
  
        OutputStream os = conn.getOutputStream();
        os.write(json2send.getBytes());
        os.flush();
  
        System.out.println("XXXXXXXX Sending request XXXXXXXX \n");
  
        if (conn.getResponseCode() != 200)
        {
          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  
          StringBuffer error = new StringBuffer();
          String inputLine;
          while ((inputLine = in.readLine()) != null)
          {
            error.append(inputLine);
          }
  
          in.close();
  
          throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode() + error.toString());
        }
    ...
  }
  </code></pre>
  
  <p>My problem is that I get the <code>responseCode</code>, but I don't know how to get the error message, which should be somewhere in the response, right? (<code>Response.status(Status.BAD_REQUEST).entity(**errorMessages**).build()</code>).</p>
  
  <p>The above code, where I check the response code, doesn't work...</p>
  
  <p>Could you please help me?</p>