users@jersey.java.net

Question about polymorphism

From: Johnson, Mike (IS) <"Johnson,>
Date: Tue, 11 Aug 2009 14:03:31 -0500

I am just starting to use Jersey. I have a problem with one of my
methods returning a list of polymorphic objects, and the Jersey client
API not being able to deserialize them correctly. Here's my situation:

@javax.xml.bind.annotation.XmlType (
  name = "theBase",
  namespace = ""
)
class TheBase
{
        public String getName();
        public void setName(String name);
}

@javax.xml.bind.annotation.XmlType (
  name = "theDerivedOne",
  namespace = ""
)
@javax.xml.bind.annotation.XmlRootElement (
  name = "theDerivedOne",
  namespace = ""
)
class TheDerivedOne : public TheBase
{
        public int getSomeInt ();
        public void setSomeInt (int value);
}

@javax.xml.bind.annotation.XmlType (
  name = "theDerivedTwo",
  namespace = ""
)
@javax.xml.bind.annotation.XmlRootElement (
  name = " theDerivedTwo ",
  namespace = ""
)
class TheDerivedTwo : public TheBase
{
        public double getSomeDouble ();
        public void setSomeDouble(double value);
}


My interface has a method that returns a class (let's call it
"TheClass") that holds a List<TheBase> member called "myThings".

The XML that is generated by Jersey looks correct to me; it basically
looks like below. Note that each of the "myThings" collection members
describes its type via its "xsi:type" attribute.

<ns2:theClass>
        <myThings xsi:type="theDerivedOne">
                <name>Name One</name>
                <someInt>5</someInt>
        </myThings>
        <myThings xsi:type="theDerivedTwo">
                <name>Another name</name>
                <someDouble>12.1</someDouble>
        </myThings>
</ns2:theClass>

Now on the client side, I am using the Jersey client API to unmarshal a
TheClass object from the method. When I do this, the objects in the
myThings list "lose" their type - they are all of type TheBase, with
only the Name property available.

com.sun.jersey.api.client.Client client =
com.sun.jersey.api.client.Client.create();
TheClass testInstance = client.resource(baseUrl).get(TheClass.class);


Any idea where I am going wrong? Does the jersey unmarshaller (which I
assume is just the JAXB unmarshaller) support polymorphic lists?

Thanks in advance,
Mike Johnson