users@jersey.java.net

Re: [Jersey] JAXB to generate JSON

From: Ronak Patel <ronak2121_at_yahoo.com>
Date: Mon, 26 Oct 2009 17:57:16 -0700 (PDT)

Paul,

I'm trying to use this API in JAX-RS/Jersey 1.0.3.1.

Seems to me that I may have to modify the default JSON Configuration?

Just doing...

final JSONJAXBContext marshallingContext = new JSONJAXBContext("com.datamodel.general");
final Marshaller marshaller = marshallingContext.createMarshaller();
// marshall the data to json in a local string
final StringWriter writer = new StringWriter();
marshaller.marshal(metadata, writer);

produces XML and NOT JSON.

Ronak




________________________________
From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
To: users_at_jersey.dev.java.net
Sent: Mon, October 26, 2009 1:26:06 AM
Subject: Re: [Jersey] JAXB to generate JSON


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.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net