title: jersey 2.0 jaxrs RI - return json string on exception
body: <p>I am creating a REST service using jersey 2.0. I am extending WebApplicationException</p>
  
  <p><strong>Method raising a particular exception</strong></p>
  
  <pre><code>if(json.equals("") || json.equals(" ")) {
        throw new ArgumentException("bad post data");
  }
  
  
        public class ArgumentException extends RestException  {
           .....
  
           public ArgumentException(String message) {
               super(Status.BAD_REQUEST,message);
           }
      }
  
  
      public class RestException extends WebApplicationException  {
      ...........
       public RestException(Status status, String message) {
  
               super(Response.status(status)
                       .entity(message)
                       .type("text/plain")
                       .build()); 
               /*
               super(Response.status(status)
                       .entity(new ErrorBean(status.getStatusCode(),message))
                       .type(MediaType.APPLICATION_JSON)
                       .build()); */
  
           }
  
  
  ErrorBean is a POJO
  </code></pre>
  
  <p>The method that returns error as plain string inside RestException works (right http code 400 and message). However when I try to pass the ErrorBean POJO and use MediaType.APPLICATION_JSON in response I get an error saying "Headers have already been sent" with http error code 500 (so some internal problem with plumbing) and empty response.</p>
  
  <p>I have also looked at this question <a href="https://stackoverflow.com/questions/2871935/returning-json-or-xml-for-exceptions-in-jersey">Returning JSON or XML for Exceptions in Jersey</a></p>
  
  <p>How can I return the exception with code and message as a JSON like </p>
  
  <p>{"code" : 400, "message" : .... }</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have received answer on SO as well as jersey users mailing list. steps are</p>
  
  <ul>
  <li>A non AJXB POJO does not need any annotations </li>
  <li>Register JacksonFeature in your application</li>
  </ul>
  
  <p>ResourceConfig rc = new ResourceConfig().packages("test").register(JacksonFeature.class);</p>