开发者

JAXB and Jersey list resolution?

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: JAXB Collections (List<T>) Use Pascal Case instead of Camel Case for Element Names


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 ;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜