2008-11-18 167 views
40

我似乎得到以下異常嘗試部署我的應用程序時:java.util.List的是一個接口,而JAXB無法處理接口

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of  IllegalAnnotationExceptions 
java.util.List is an interface, and JAXB can't handle interfaces. 
this problem is related to the following location: 
    at java.util.List 
    at private java.util.List  foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return 
    at  foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse 
java.util.List does not have a no-arg default constructor. 
    this problem is related to the following location: 
     at java.util.List 
     at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return 
    at  foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse 


我的代碼工作得很好,直到我改變從列表中返回類型列出清單< <RelationCanonical> >

下面是部分Web服務:


@Name("relationService") 
@Stateless 
@WebService(name = "RelationService", serviceName = "RelationService") 
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) 
public class RelationService implements RelationServiceLocal { 

    private boolean login(String username, String password) { 
     Identity.instance().setUsername(username); 
     Identity.instance().setPassword(password); 
     Identity.instance().login(); 
     return Identity.instance().isLoggedIn(); 
    } 

    private boolean logout() { 
     Identity.instance().logout(); 
     return !Identity.instance().isLoggedIn(); 
    } 

    @WebMethod 
    public List<List<RelationCanonical>> getRelationsFromPerson(@WebParam(name = "username") 
    String username, @WebParam(name = "password") 
    String password, @WebParam(name = "foedselsnummer") 
    String... foedselsnummer) { 

...... 
...... 
...... 
} 


我也嘗試通過刪除@SOAPBinding並嘗試默認,但會發生相同的結果。 感謝所有幫助

UPDATE

我要指出了一些東西。我將所有List更改爲ArrayList,然後編譯。我說爲什麼編譯而不工作是因爲它表現奇怪。我得到一個類型爲Object的對象: RelationServiceStub.ArrayList 但該對象沒有get方法或者不像List那樣工作。我也試圖把它放到一個列表中,但沒有奏效。

請注意,這是我使用Axis 2和wsdl2java之後所以是的,現在它編譯,但我不知道如何獲取數據。

回答

24

根據我的理解,您將無法通過JAXB處理普通List,因爲JAXB不知道如何將其轉換爲XML。

相反,你需要定義其持有List<RelationCanonical>一個JAXB類型(我稱之爲Type1),而另一個持有這些類型的列表,依次(如你正在處理一個List<List<...>>;我會稱這種類型爲Type2)。然後

結果可能是一個XML輸出中是這樣的:

<Type2 ...> 
    <Type1 ...> 
     <RelationCanonical ...> ... </RelationCanonical> 
     <RelationCanonical ...> ... </RelationCanonical> 
     ... 
    </Type1> 
    <Type1> 
     <RelationCanonical ...> ... </RelationCanonical> 
     <RelationCanonical ...> ... </RelationCanonical> 
     ... 
    </Type1> 
    ... 
</Type2> 

如果沒有這兩個封裝JAXB註釋類型,JAXB的處理器不知道要生成的標記,因而失敗。

- 編輯:

我的意思應該看起來有點像這樣:

@XmlType 
public class Type1{ 

    private List<RelationCanonical> relations; 

    @XmlElement 
    public List<RelationCanonical> getRelations(){ 
     return this.relations; 
    } 

    public void setRelations(List<RelationCanonical> relations){ 
     this.relations = relations; 
    } 
} 

@XmlRootElement 
public class Type2{ 

    private List<Type1> type1s; 

    @XmlElement 
    public List<Type1> getType1s(){ 
     return this.type1s; 
    } 

    public void setType1s(List<Type1> type1s){ 
     this.type1s= type1s; 
    } 
} 

您也應該檢查JAXB section in the J5EE tutorialUnofficial JAXB Guide

8

如果適合你的目的,你總是可以這樣定義一個數組:

YourType[] 

JAXB肯定能弄清楚,是什麼,你應該能夠立刻使用它的客戶端。我也建議你這樣做,因爲你不應該能夠通過列表修改從服務器檢索的數組,但通過Web服務提供的方法

1

如果你想這樣做的任何類。

return items.size() > 0 ? items.toArray((Object[]) Array.newInstance(
      items.get(0).getClass(), 0)) : new Object[0]; 
-6

您可以使用「的ArrayList」,而不是內部的「清單」

+1

是的,我在我做這個問題已經提到的,但它仍然沒有奏效。閱讀更新 – 2013-09-13 13:01:04

相關問題