2010-02-05 42 views
2

我有下面的XSD定义来生成一些jaxb对象。它运作良好。JAXB和泽西岛名单解析?

<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> 

我使用Spring RowMapper将我的数据库中的行映射到Person对象。所以,我结束 与列表<人>对象,这是不是一个人的对象。我人对象有一个列表<内部的人>。

然后在我的球衣资源类,我有:

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

这是返回的XML是:

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

我的问题是它是如何从列表<使得映射到人>到XML中的人。此外,元素是“人”(大写P)而不是“人”(小写字母P)。看起来它根本没有真正使用XSD。

编辑这在某种程度上与此相关的问题:JAXB Collections (List<T>) Use Pascal Case instead of Camel Case for Element Names

回答

3

好像它没有真正使用 XSD在所有

这是因为它没有。 JAXB仅在使用XJC生成代码时使用该模式;在此之后它就没有用处了,它在运行时只会使用注释(它也可以用于验证,但在这里并不相关)。

您的REST方法正在返回List<Person>,并且Jersey正在尽最大努力将它转换为XML,方法是将其包装在<people>中。你还没有告诉它使用包装类,它无法为自己猜测。

如果你想生成<People>包装元素,那么你需要给它People包装类:

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

    return people ; 
}