views:

66

answers:

1

I have the following XSD defined to generate some jaxb objects. It works well.

<xsd:element name="Person" type="Person" />

<xsd:complexType name="Person">
    <xsd:sequence>
        <xsd:element name="Id" type="xsd:int" />
        <xsd:element name="firstName" type="xsd:string" />
        <xsd:element name="lastName" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="People">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Person" minOccurs="0" maxOccurs="unbounded"
                type="Person" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

I use Spring RowMapper to map the rows from my database into Person objects. So, I end up with a List<Person> object, which is not a People object. I People object has a List<Person> internally.

Then in my Jersey resource class I have:

@GET
@Path("/TheListOfPeople")
public List<Person> getListOfPeople() { 
    List<Person> list = dao.getList();
    return list;
}

The XML which is returned is:

<?xml version="1.0" encoding="UTF-8" standalone="yes" >
<people>
  <Person>...</Person>
  <Person>...</Person>
  <Person>...</Person>
  <Person>...</Person>
</people>

My question is how is it making the mapping to from List<Person> to People in the XML. Also, The element is "People" (capital P) not "people" (lowercase P). Seems like it's not really using the XSD at all.

EDIT This is somehow related to this question: http://stackoverflow.com/questions/1545041

+1  A: 

Seems like it's not really using the XSD at all

That's because it doesn't. JAXB only uses the schema when generated the code using XJC; after that it has no use for it, at runtime it only uses the annotations (it can also use it for validation, but that's not relevant here).

Your REST method is returning List<Person>, and Jersey is doing its best to turn that into XML, by wrapping it in <people>. You haven't told it to use the People wrapper class, and it has no way of guessing that for itself.

If you want to generate the <People> wrapper element, then you need to give it the People wrapper class:

@GET
@Path("/TheListOfPeople")
public People getListOfPeople() { 
    People people = new People();
    people.getPerson().addAll(dao.getList()); // or something like it

    return people ;
}
skaffman