users@jersey.java.net

Re: [Jersey] Pointers on using JAXB with Jersey

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 21 Aug 2008 11:25:27 +0200

Only a small correction inline...

On Thu, Aug 21, 2008 at 09:39:02AM +0200, Paul Sandoz wrote:
> 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

It should be @XMLRootElement instead.

> 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.

I wanted the default mapping to be the least verbose, if it's not the case, then it is a bug.

~Jakub

>
> 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.
> >
> >
> >
>

-- 
http://blogs.sun.com/japod