title: JAX-RS — How to return JSON and HTTP status code together?
body: <p>I'm writing a REST web app (NetBeans 6.9, JAX-RS, TopLink Essentials) and trying to return JSON <strong>and</strong> HTTP status code. I have code ready and working that returns JSON when the HTTP GET method is called from the client. Essentially:</p>
  
  <pre><code>@Path("get/id")
  @GET
  @Produces("application/json")
  public M_機械 getMachineToUpdate(@PathParam("id") String id) {
  
      // some code to return JSON ...
  
      return myJson;
  }
  </code></pre>
  
  <p>But I <em>also</em> want to return an HTTP status code (500, 200, 204, etc.) along with the JSON data.</p>
  
  <p>I tried to use <code>HttpServletResponse</code>:</p>
  
  <pre><code>response.sendError("error message", 500);
  </code></pre>
  
  <p>But this made the browser think it's a "real" 500 so the output web page was a regular HTTP 500 error page.</p>
  
  <p>I want to return an HTTP status code so that my client-side JavaScript can handle some logic depending on it (to e.g. display the error code and message on an HTML page). Is this possible or should HTTP status codes not be used for such thing?</p>