users@jersey.java.net

Re: [Jersey] Pointers on using JAXB with Jersey

From: Kevin Duffey <andjarnic_at_yahoo.com>
Date: Thu, 21 Aug 2008 13:52:24 -0700 (PDT)

Thanks Paul. I think I got the general idea.. but one thing I am not sure of.. since I have JAXB generated classes (they have the XMLElement, XMLType, XMLAccessorType defined in the generated source), and I include that .jar that contains those in the WEB-INF/lib of my REST app, do I need Provider classes as well, or does the Jersey/JAXB stuff know how to grab an instance of my generated class and return it as xml? As well, I am slightly confused on the ability for it to return xml or json... does it know how to do both for me already such that if the request Accept header says application/json, it returns that.

So I tried sending in a simple snippet of xml according to my xsd, but I haven't quite seen the method called yet. Getting various parser errors, like java.lang.IllegalArgumentException: java.text.ParseException: Expected separator '=' instead of '/'
and the other one was MediaType not found for the request but I think I got that one worked out. I am using that Swing program called WizTools.org RESTclient 2.1.. so I select POST, give the url to my resource, and send in a snippet of xml. I have this feeling I am missing something small... just not sure what yet, but will keep pluggin away.

Thanks.







----- Original Message ----
From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
To: users_at_jersey.dev.java.net
Sent: Thursday, August 21, 2008 12:39:02 AM
Subject: Re: [Jersey] Pointers on using JAXB with Jersey

Hi Kevin,

There is an example in the distribution for using JAXB with JSON.

Just use the JAXB element directly:

   @POST
   @Consumes("application/xml")
   public void postJAXBBean(MyJAXBBean b) { ... }



   @GET
   @Produces("application/xml")
   public MyJAXBBean getJAXBBean() { ... }


If you want JSON do this:

   @POST
   @Consumes({"application/xml", "application/json"})
   public void postJAXBBean(MyJAXBBean b) { ... }



   @GET
   @Produces({"application/xml", "application/json"})
   public MyJAXBBean getJAXBBean() { ... }


It is important that MuJAXBBean is annotated with @XMLElement. If you want to use a JAXB type you can do:

   @POST
   @Consumes({"application/xml", "application/json"})
   public void postJAXBType(JAXBElement<MyJAXBType> b) { ... }



   @GET
   @Produces({"application/xml", "application/json"})
   public JAXBElement<MyJAXBType> getJAXBType() { ... }


It is possible to configure the JSON output as well (search this list for JSONJAXBContext) as often the default mapping of JAXB beans to JSON can be too verbose or not quite right for JSON consumers.

Paul.

On Aug 21, 2008, at 2:30 AM, Kevin Duffey wrote:

Hey all,

Is there some good examples or links that explain a bit more how to make use of JAXB/schema generated objects to handle XML requests and return XML responses. I have a project generating my model classes using ant/Jaxb, and from my understanding there is a way to use resources and providers to automatically handle xml sent to resources so that I myself do not have to do any sort of parsing, I can instead access the generated object tree to deal with the request xml sent in, and as well, I can manipulate an instance of the model object tree and send it back as a xml response. It seems like a great way to handle xml anyway.

Is there a JSON equivalent for handling json using objects and sending json back as a response.. preferably using the same JAXB generated object tree?

Thank you.