users@jersey.java.net

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

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Tue, 26 Jan 2010 15:31:21 +0100

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