2010-07-13 56 views
30

當我調用一個特殊的寧靜服務方法時,它使用CXF構建,我得到以下錯誤,任何人都知道爲什麼以及如何解決它?JAXB例外:本類不知道的類

發生JAXBException:類 com.octory.ws.dto。 ProfileDto還是其超類是已知的這個 方面的任何 ...

以下是服務方法和相關的DTO:

public class Service { 
    public Response results() { 
    Collection<ProfileDto> profilesDto = new ArrayList<ProfileDto>(); 
    ... 
    SearchResultDto srd = new SearchResultDto(); 
    srd.setResultEntities(profilesDto); // Setting profilesDto collection as resultEntities 
    srd.setResultSize(resultSize); 
    return Response.ok(srd).build(); 
    } 
} 

SearchResultDto:

@XmlRootElement(name="searchResult") 
public class SearchResultDto { 
    private Collection resultEntities; 
    private int resultSize; 

    public SearchResultDto() { } 

    @XmlElementWrapper(name="resultEntities") 
    public Collection getResultEntities() { 
     return resultEntities; 
    } 

    public void setResultEntities(Collection resultEntities) { 
     this.resultEntities = resultEntities; 
    } 

    public int getResultSize() { 
     return resultSize; 
    } 

    public void setResultSize(int resultSize) { 
     this.resultSize = resultSize; 
    } 
} 

ProfileDto:

@XmlRootElement(name="profile") 
public class ProfileDto { 
    ... 
    ... 
    public ProfileDto() { } 
    ... 
} 

回答

33

ProfileDto類不SearchResultDto引用。嘗試將@XmlSeeAlso(ProfileDto.class)添加到SearchResultDto

+0

添加@XmlSee也解決了問題;我在印象之下只有當被引用的類是一個子類時才需要註釋。謝謝。 – ABK07 2010-07-13 18:48:09

+4

如果班級是'SearchResultDto ',那麼T是通用的呢? – 2012-08-16 22:30:39

+4

@Hendy Irawan - 也可以將註釋添加到Web服務本身(即在「@ WebService」註釋之後)。如果您正在處理泛型,那麼在該階段聲明其他類型更有意義,您應該知道全部類型。 – CurtainDog 2013-04-29 05:44:24

0

我在Tomcat上遇到了同樣的異常。我發現了另一個問題 - 當我使用wsimport over Maven插件爲多個WSDL生成存根時 - 類ObjectFactory(存根引用此類)僅包含一個wsdl的方法。因此,您應該將一個ObjectFactory類(對於每個WSDL)中的所有方法進行合併,或者在不同目錄中生成每個wsdl存根(將分隔ObjectFactory類)。它解決了問題,對我來說這個exception..J

24

,因爲我註冊了錯誤的類在這行代碼我有此錯誤:

JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class); 
3

此錯誤消息偏偏可能是因爲您的ProfileDto類沒有註冊在JAXB內容中,或者使用它的類不使用@XmlSeeAlso(ProfileDto.class)以使JAXB可處理。

關於您的評論:

我的印象是註釋只需要 時引用的類是一個子類。不,他們還需要

當JAXB上下文或不申報,例如,當具有靜態參考它的唯一一類具有此引用與註釋@XmlTransient。我保持教程here

+0

感謝您的教程。在這個答案中,「內容」應該是「上下文」嗎? – djb 2013-01-14 18:41:19

2

類名設置爲JAXB編組的財產「classesToBeBound」固定它:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="classesToBeBound"> 
      <list> 
       <value>myclass</value> 
      </list> 
     </property> 
</bean> 
3

我與春天啓動了同樣的問題。它解決了當我設置包裹到編組。

@Bean 
public Jaxb2Marshaller marshaller() throws Exception 
{ 
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); 
    marshaller.setPackagesToScan("com.octory.ws.dto"); 
    return marshaller; 
} 

@Bean 
public WebServiceTemplate webServiceTemplate(final Jaxb2Marshaller marshaller) 
{ 
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); 
    webServiceTemplate.setMarshaller(marshaller); 
    webServiceTemplate.setUnmarshaller(marshaller); 
    return webServiceTemplate; 
}