2010-07-13 55 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; 
}