users@jersey.java.net

Re: [Jersey] JAXB to generate JSON

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 26 Oct 2009 09:26:06 +0100

On Oct 24, 2009, at 3:03 AM, Ronak Patel wrote:

> Hi All,
>
> I'm aware of the standard way for JAX-RS/Jersey and JAXB to generate
> JSON as a response to a web service invocation.
>
> I'm also aware that I can send a JAXB Pojo to the Jersey Client API
> to send JSON to a JSON REST Web Service.
>
> However, I'm wondering...can I ask JAX-RS/Jersey or JAXB to generate
> a java.lang.String of the JSON given a Pojo in my own code?
>
> If this is possible, how do I do that?
>

Yes.

See JSONJAXBContext:

   https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/json/JSONJAXBContext.html

   JSONJAXBContext c = ...
   JSONMarshaller m = createJSONMarshaller();
   MyJAXBBean b = ...
   StringWriter w = ...
   m.marshallToJSON(b, w);

Paul.