users@jersey.java.net

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

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Wed, 27 Jan 2010 18:27:40 +0100

On Wed, Jan 27, 2010 at 06:21:49PM +0100, Jakub Podlesak wrote:
>
> Hi Ed,
>
> Please see the enclosed e-mail as a response to your previous post.
> I sent it directly to the jersey mailing list originally.
> Maybe you missed it, because you had not been subsribed there?
>
> And to summarize a bit: the default Jersey JSON notation for JAXB beans
> *does* support nested complex types (and this is being continuously tested).
> There might be an issue with the Geronimo environment, but i am not sure about
> that, as we do not run the Jersey JSON tests there.
>
> To increase the response time, if you have an issue, is to file the issue

I meant to decrease it, i.e. to speed things up.

~Jakub

> at the Jersey project home page [1], and *attach a reproducible test case*.
>
> Thanks,
>
> ~Jakub
>
> [1]https://jersey.dev.java.net
>
> On Wed, Jan 27, 2010 at 09:55:32AM -0600, EWILLIAMS_at_CERNER.COM wrote:
> > I haven't received any response from you on the initial email, but I have done more investigation. It looks like the issue has nothing to do with arrays and is more about nested complex types.
> > I'm able to reproduce my JSON unmarshalling (@POST) problem with the following simple schema which JAXB generates objects for.
> > <element name="carsHolder">
> > <complexType>
> > <sequence>
> > <element name="dog" type="tns:Dog"/>
> > </sequence>
> > </complexType>
> > </element>
> > Where Dog is defined as:
> > <complexType name="Dog">
> > <sequence>
> > <element name="id" type="long" minOccurs="1"/>
> > </sequence>
> > </complexType>
> >
> > Here is the data passed in, which is exactly what is returned from a corresponding @GET of the carsHolder:
> > {
> > "dog" : {
> > "id" : 1234
> > }
> > }
> >
> >
> > I can see the unmarshalling get to the element called "id". This is where things seem to go unexpected in the "StructureLoader" class.
> >
> > @Override
> > public void childElement(UnmarshallingContext.State state, TagName arg) throws SAXException {
> > ChildLoader child = childUnmarshallers.get(arg.uri,arg.local);
> > if(child==null) {
> > child = catchAll;
> > if(child==null) {
> > super.childElement(state,arg);
> > return;
> > }
> > }
> >
> > state.loader = child.loader;
> > state.receiver = child.receiver;
> > }
> >
> > arg.uri = "" and arg.local = "id", and child returns null from childUnmarshallers.get(arg.uri, arg.local);
> > Ultimately, it is passed up to the super and an unexpected child element is reported - whereby the whole thing appears to be ignored (no "id" object created, and predictor.expectText returns false for the value).
> > In the end, I end up with a "CarsHolder object" which has a "Dog object" inside of it, with the property "id" equal to 0 (rather than the expected 1234). I can get no property at this level to get unmarshalled properly (only tried simple types).
> >
> > Can you tell me where to go with this issue, please?!
> > Thank you,
> > Ed
> >
> >
> > _____________________________________________
> > From: Williams,Ed
> > Sent: Friday, January 22, 2010 10:21 AM
> > To: 'users_at_jersey.dev.java.net'
> > Subject: Jersey JSON unmarshalling of array elements.
> >
> >
> > 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

> Date: Tue, 26 Jan 2010 15:31:21 +0100
> From: Jakub Podlesak <Jakub.Podlesak_at_sun.com>
> Subject: Re: [Jersey] Jersey JSON unmarshalling of array elements.
> To: users_at_jersey.dev.java.net
> User-Agent: "Mutt 1.5.7i/SunOS 5.11"
>
>
> 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