2010-02-05 141 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 ; 
}