users@jersey.java.net

Re: [Jersey] Jersey JSON unmarshalling of array elements.

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Fri, 29 Jan 2010 09:38:32 +0100

On Thu, Jan 28, 2010 at 05:24:12PM -0600, EWILLIAMS_at_CERNER.COM wrote:
> I switched over to explicitly registering the JSONJacksonProvider, and this doesn't appear to be an issue any more.
> Perhaps it doesn't use the package-info class?

This is really strange, as the thing worked on my side even with the package-info class.
Have you touched the file in any sense (namespace update or something similar)?.

Anyway, i am glad you worked out a solution.

~Jakub

>
> Thanks for your help Jakub.
>
> -----Original Message-----
> From: Williams,Ed
> Sent: Thursday, January 28, 2010 1:33 PM
> To: 'users_at_jersey.dev.java.net'
> Subject: RE: [Jersey] Jersey JSON unmarshalling of array elements.
>
> Jakub,
> I realized that the difference is in the existence of the package-info.java file within my package.
> If I remove this file, everything begins to deserialize.
> Alternatively, if I remove this line:
> elementFormDefault="qualified"
> from my schema, all seems to work.
> Do you know what functionality is lost by removing this (or why this is necessary)?
>
>
>
> -----Original Message-----
> From: Jakub.Podlesak_at_Sun.COM [mailto:Jakub.Podlesak_at_Sun.COM]
> Sent: Thursday, January 28, 2010 11:29 AM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Jersey JSON unmarshalling of array elements.
>
> On Wed, Jan 27, 2010 at 03:33:54PM -0600, EWILLIAMS_at_CERNER.COM wrote:
> > Ok Jakub,
> > Your classes worked, which prompted me to understand the differences between yours and my classes.
> > So upon further investigation, I simplified my classes down to these two attached schema-generated classes (Dog and DogsHolder - source shown at the bottom).
> > And here is the input (taken from the @GET output):
> > {
> > "dog" : {
> > "id" : 1234
> > }
> > }
> >
> > Here is the crazy part that you may understand:
> > 1. Before I remembered to list the two classes in my JAXBContextResolver (configured for natural), Dog instance inside of DogsHolder was unmarshalling to null.
> > After I remembered, I was back to having a Dog instance, with an id of 0.
> > 2. If I copy the jaxb generated classes to a non-generated package and
> > called them Dog1 and DogsHolder1, and then create resource methods for
> > accessing them -everything seems to work!! (which explains why your
> > classes work)
> >
> > So something about the whole generation from schema (maybe namespacing/package?) is causing it to unmarshall improperly.
> >
> > Any informed thoughts?
>
> I dunno, please try the attached project, where the java sources are generated from the schema at src/main/xsd/test.xsd
>
> mvn clean verify should generate st. like:
> Jan 28, 2010 6:23:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 1 * Client in-bound response
> 1 < 200
> 1 < Transfer-Encoding: chunked
> 1 < Date: Thu, 28 Jan 2010 17:23:07 GMT
> 1 < server: grizzly/1.9.8
> 1 < Content-Type: application/json
> 1 <
> {"dog":{"id":1234}}
>
> Jan 28, 2010 6:23:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 2 * Client out-bound request
> 2 > PUT http://localhost:9998/holder
> 2 > Content-Type: application/json
> 2 >
> {"dog":{"id":3456}}
>
> Jan 28, 2010 6:23:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 2 * Client in-bound response
> 2 < 204
> 2 < Date: Thu, 28 Jan 2010 17:23:07 GMT
> 2 < server: grizzly/1.9.8
> 2 <
>
> Jan 28, 2010 6:23:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 3 * Client out-bound request
> 3 > GET http://localhost:9998/holder
> 3 > Accept: application/json
> 3 >
>
> Jan 28, 2010 6:23:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 3 * Client in-bound response
> 3 < 200
> 3 < Transfer-Encoding: chunked
> 3 < Date: Thu, 28 Jan 2010 17:23:07 GMT
> 3 < server: grizzly/1.9.8
> 3 < Content-Type: application/json
> 3 <
> {"dog":{"id":3456}}
>
>
> You may want to try with your schema file.
>
> ~Jakub
>
>
> >
> > Thank you,
> > Ed
> >
> >
> > //
> > // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833
> > // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
> > // Any modifications to this file will be lost upon recompilation of the source schema.
> > // Generated on: 2010.01.27 at 02:41:40 PM CST
> > //
> >
> >
> > package com.theserverlabs.namespaces.cars;
> >
> > import javax.xml.bind.annotation.XmlAccessType;
> > import javax.xml.bind.annotation.XmlAccessorType;
> > import javax.xml.bind.annotation.XmlElement;
> > import javax.xml.bind.annotation.XmlRootElement;
> > import javax.xml.bind.annotation.XmlType;
> >
> >
> > /**
> > * <p>Java class for anonymous complex type.
> > *
> > * <p>The following schema fragment specifies the expected content contained within this class.
> > *
> > * <pre>
> > * &lt;complexType>
> > * &lt;complexContent>
> > * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
> > * &lt;sequence>
> > * &lt;element name="dog" type="{http://www.theserverlabs.com/namespaces/cars}Dog"/>
> > * &lt;/sequence>
> > * &lt;/restriction>
> > * &lt;/complexContent>
> > * &lt;/complexType>
> > * </pre>
> > *
> > *
> > */
> > @XmlAccessorType(XmlAccessType.FIELD)
> > @XmlType(name = "", propOrder = {
> > "dog"
> > })
> > @XmlRootElement(name = "dogsHolder")
> > public class DogsHolder {
> >
> > @XmlElement(required = true)
> > protected Dog dog;
> >
> > /**
> > * Gets the value of the dog property.
> > *
> > * @return
> > * possible object is
> > * {_at_link Dog }
> > *
> > */
> > public Dog getDog() {
> > return dog;
> > }
> >
> > /**
> > * Sets the value of the dog property.
> > *
> > * @param value
> > * allowed object is
> > * {_at_link Dog }
> > *
> > */
> > public void setDog(Dog value) {
> > this.dog = value;
> > }
> >
> > }
> >
> > //
> > // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833
> > // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
> > // Any modifications to this file will be lost upon recompilation of the source schema.
> > // Generated on: 2010.01.27 at 02:41:40 PM CST
> > //
> >
> >
> > package com.theserverlabs.namespaces.cars;
> >
> > import javax.xml.bind.annotation.XmlAccessType;
> > import javax.xml.bind.annotation.XmlAccessorType;
> > import javax.xml.bind.annotation.XmlType;
> >
> >
> > /**
> > * <p>Java class for Dog complex type.
> > *
> > * <p>The following schema fragment specifies the expected content contained within this class.
> > *
> > * <pre>
> > * &lt;complexType name="Dog">
> > * &lt;complexContent>
> > * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
> > * &lt;sequence>
> > * &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}long"/>
> > * &lt;/sequence>
> > * &lt;/restriction>
> > * &lt;/complexContent>
> > * &lt;/complexType>
> > * </pre>
> > *
> > *
> > */
> > @XmlAccessorType(XmlAccessType.FIELD)
> > @XmlType(name = "Dog", propOrder = {
> > "id"
> > })
> > public class Dog {
> >
> > protected long id;
> >
> > /**
> > * Gets the value of the id property.
> > *
> > */
> > public long getId() {
> > return id;
> > }
> >
> > /**
> > * Sets the value of the id property.
> > *
> > */
> > public void setId(long value) {
> > this.id = value;
> > }
> >
> > }
> >
> >
> >
> >
> >
> > -----Original Message-----
> > From: Jakub.Podlesak_at_Sun.COM [mailto:Jakub.Podlesak_at_Sun.COM]
> > Sent: Tuesday, January 26, 2010 8:31 AM
> > To: users_at_jersey.dev.java.net
> > Subject: Re: [Jersey] Jersey JSON unmarshalling of array elements.
> >
> >
> > Hi,
> >
> > It would be helpful to see the source code for your JAXB beans and web resource classes.
> >
> > Anyway, based on your information, i do not see what could be wrong here.
> > Have you tried the same scenario with the XML representation? Does it work?
> >
> > Using the following JAXB bean:
> >
> > @XmlRootElement
> > public class AllergyRecord {
> >
> > @XmlType
> > public static class AllergyTest {
> >
> > public String allergyId;
> > public String type;
> >
> > public AllergyTest(){};
> >
> > public AllergyTest(String allergyId, String type) {
> > this.allergyId = allergyId;
> > this.type = type;
> > }
> > }
> >
> > public String holderId;
> > public List<AllergyTest> tests;
> > }
> >
> > And the allergy web resource:
> >
> > @Path("allergy")
> > public class AllergyResource {
> >
> > @GET @Produces(MediaType.APPLICATION_JSON)
> > public AllergyRecord getAlergy() {
> >
> > AllergyRecord result = new AllergyRecord();
> >
> > result.holderId = "6666";
> > result.tests = new LinkedList<AllergyRecord.AllergyTest>();
> > result.tests.add(new AllergyTest("1111", "firstOne"));
> > result.tests.add(new AllergyTest("2222", "secondOne"));
> >
> > return result;
> > }
> >
> >
> > @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
> > public AllergyRecord postAlergy(AllergyRecord rec) {
> >
> > rec.holderId = "6667";
> > rec.tests.get(0).allergyId = "1112";
> > rec.tests.get(1).allergyId = "2223";
> >
> > return rec;
> > }
> > }
> >
> > The following test is passing for me:
> >
> > ClientConfig cc = new DefaultClientConfig();
> > Client c = Client.create(cc);
> > WebResource r = c.resource(Main.BASE_URI);
> >
> > AllergyRecord rec = r.path("allergy")
> > .accept("application/json")
> > .get(AllergyRecord.class);
> >
> > AllergyRecord recFromPost = r.path("allergy")
> > .type(MediaType.APPLICATION_JSON)
> > .entity(rec)
> > .accept(MediaType.APPLICATION_JSON)
> > .post(AllergyRecord.class);
> >
> > assertEquals("Allergy rec returned with wrong allergy test #1",
> > "1112", recFromPost.tests.get(0).allergyId);
> >
> > Does the above work for you in the Geronimo environment?
> >
> > ~Jakub
> >
> >
> > On Fri, Jan 22, 2010 at 10:21:01AM -0600, EWILLIAMS_at_CERNER.COM wrote:
> > > Hopefully, you can point me to the right people if you do not handle JSON questions.
> > >
> > > I'm fairly new to Jersey, and have been producing/consuming xml for the most part.
> > > I am using: jersey-json 1.1.4.1, jersey-server 1.1.4.1 and jaxb-impl 2.1.12.
> > > I am using all defaults (no JSON provider, mapped
> > >
> > > The issue:
> > > The JSON I produce from a very simple test @GET does not get correctly unmarshalled when I pass it in to my @POST.
> > > Specifically, the root object is created
> > >
> > > Here is the JSON:
> > >
> > > {
> > > "holderId" : "6666",
> > > "tests" : [
> > > {
> > > "allergyId" : "1111",
> > > "type" : "firstOne"
> > > },
> > > {
> > > "allergyId" : "2222",
> > > "type" : "secondOne"
> > > }
> > > ]
> > > }
> > >
> > >
> > > So there is an outer (JAXB-generated) class called TestArrayHolder which is created, but no properties are populated!
> > > The marshalling seems to work like a charm, but I can get anything to unmarshall...
> > >
> > > One last note that could be related: I run on Geronimo, so I had to add the hidden classes to get the JSON marshalling to pick up my project's xml/binding jars.
> > > <sys:hidden-classes>
> > > <filter>com.sun.xml</filter>
> > > <filter>javax.xml.bind</filter>
> > > </sys:hidden-classes>
> > >
> > > I'm not sure what else to try from here. Can you help?
> > >
> > > Thank you,
> > > Ed
> > >
> > >
> > > ----------------------------------------------------------------------
> > > CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.
> >
> > --
> > http://blogs.sun.com/japod
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
> --
> http://blogs.sun.com/japod
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

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