users@jersey.java.net

Re: [Jersey] JAXB JSON Marshalling/Unmarshalling

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Fri, 22 May 2009 13:46:30 +0200

Hi Samuel Ni,

i do not see the attached code. However, you might want to try
to modify the json-from-jaxb example a bit, to see how the thing works:

at com.sun.jersey.samples.jsonfromjaxb.jaxb.Flights class, modify the @XmlRootElement annotation to (line 79) put an explicit namespace there:

@XmlRootElement(name = "flights", namespace="http://jersey.dev.java.net")

at com.sun.jersey.samples.jsonfromjaxb.config.JAXBContextResolver class,
modify the creation of the conext (line 68), with:

        this.context = new JSONJAXBContext(JSONConfiguration.mappedJettison().xml2JsonNs(new HashMap<String,String>(){{put("http://jersey.dev.java.net", "jersey");}}).build(), cTypes);

now run the app and try to:

% curl -Haccept:application/json -i http://localhost:9998/jsonfromjaxb/flights
HTTP/1.1 200 OK
server: grizzly/1.9.8
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 22 May 2009 11:39:29 GMT

{"jersey.flights":{"flight":[{"flightId":"OK123","company":"Czech Airlines","number":123,"aircraft":"B737"},{"flightId":"OK124","company":"Czech Airlines","number":124,"aircraft":"AB115"}]}}


You can see the "jersey.flights" JSON name incorporates the mapped namespace.

I suspect your JAXBContexResolver#getContext method does not return
the right JSONJAXBContext instance,
or you are missing the @Provider annotation there, or something else
is going wrong, which is really hard to tell without access to the whole codebase
of your app.

HTH,

~Jakub


On Fri, May 22, 2009 at 06:01:15PM +0800, Ni Yue wrote:
> Jakub,
>
> Thanks for your quick reply.
> I checked out the sample link you send but still can not figure out what's
> the problem. The only difference is I used mapped jettison notation in
> JAXBContentResolver, and add a namespaceMap:
>
> public JAXBContextResolver() throws Exception {
> > Map<String, String> namespaceMap = new HashMap<String, String>();
> > namespaceMap.put("http://www.w3.org/2001/XMLSchema", "xs");
> > namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance",
> > "xsi");
> > namespaceMap.put("http://core.example.com", "core");
> > JSONConfiguration config =
> > JSONConfiguration.mappedJettison().xml2JsonNs(namespaceMap).build();
> > this.m_context = new JSONJAXBContext(
> > config,
> > m_types);
> > }
> >
>
> I am not sure I miss something or not. I attached Jersey's source code,
> debugged and found even if I set mappedJettison configuration in
> JAXBContextResolver, in JSONRootElementProvider, the Marshaller in writeTo
> method is not instance of JSONMarshaller, and it will use
> JsonXmlStreamWriter to produce mapped notation.
>
> > @Override
> > protected void writeTo(Object t, MediaType mediaType, Charset c,
> > Marshaller m, OutputStream entityStream)
> > throws JAXBException, IOException {
> > if (m instanceof JSONMarshaller) {
> > JSONMarshaller jm = (JSONMarshaller)m;
> > jm.setJsonEnabled(true);
> > jm.marshal(t,
> > new OutputStreamWriter(entityStream, c));
> > } else {
> > m.marshal(t, JsonXmlStreamWriter.createWriter(
> > new OutputStreamWriter(entityStream, c), true));
> > }
> > }
>
>
> Any ideas? Thanks.
>
> 2009/5/22 Jakub Podlesak <Jakub.Podlesak_at_sun.com>
>
> > On Fri, May 22, 2009 at 04:54:58PM +0800, Ni Yue wrote:
> > > Hello everyone,
> > >
> > > I am using Jersey to serve JSON request/response and met some issue.
> > Because
> > > in my xml schema, there're multiple namespaces, and I would like to use
> > > mapped jettison notation to produce JSON response. But it seems mapped
> > > notation is always used during marshalling to JSON even if I specified
> > > mapped jettison configuration in my JAXBContextResolver. How can I make
> > > Jersey to use different notation during producing JSON response? I found
> >
> > The mechanism should work fine on the server side. See [1] for an example.
> > For the client side you need to explicitly register the context resover,
> > but i suspect, it is not your case, right?
> >
> > Anyway, without seeing your code, it is hard to say, what is wrong.
> > Feel free to send me a private e-mail revealing more details, so that
> > i can help you.
> >
> > Thanks,
> >
> > ~Jakub
> >
> > [1]
> > http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.1.0-ea/json-from-jaxb-1.1.0-ea-project.zip
> >
> > > only when unmarshalling JSON request, different JSON unmarshaller will be
> > > pickuped according to notation. I am using Jersey 1.0.3. Thanks in
> > advance.
> > >
> > > Regards,
> > > Samuel Ni
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
> >