rest - jersey 2.0 jaxrs RI - return json string on exception -


i creating rest service using jersey 2.0. extending webapplicationexception

method raising particular exception

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 pojo 

the method returns error plain string inside restexception works (right http code 400 , message). when try pass errorbean pojo , use mediatype.application_json in response error saying "headers have been sent" http error code 500 (so internal problem plumbing) , empty response.

i have looked @ question returning json or xml exceptions in jersey

how can return exception code , message json

{"code" : 400, "message" : .... }

update

i have received answer on jersey users mailing list. steps are

  • a non ajxb pojo not need annotations
  • register jacksonfeature in application

resourceconfig rc = new resourceconfig().packages("test").register(jacksonfeature.class);

you need register jacksonfeature in application/resourceconfig, i.e.:

// create jax-rs application. final application application = new resourceconfig()         .packages("org.glassfish.jersey.examples.jackson")         .register(jacksonfeature.class)         // no need register provider if no special configuration required.         .register(myobjectmapperprovider.class); 

take @ documentation jackson support in jersey , @ example.


Comments