Java JAXB 异常:此上下文中未知的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3237473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Exception: Class not known to this context
提问by ABK07
When I call a particular restful service method, which is built using CXF, I get the following error, anyone know why and how to resolve it?
当我调用使用CXF构建的特定 Restful 服务方法时,出现以下错误,有人知道为什么以及如何解决吗?
JAXBException occurred : class com.octory.ws.dto.ProfileDtonor any of its super class is known to this context...
发生 JAXBException:类 com.octory.ws.dto。ProfileDto或其任何超类在此上下文中都是已知的...
Following are the service method and relevant DTOs:
以下是服务方法和相关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:
搜索结果Dto:
@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() { }
...
}
采纳答案by lexicore
Your ProfileDto
class is not referenced in SearchResultDto
. Try adding @XmlSeeAlso(ProfileDto.class)
to SearchResultDto
.
ProfileDto
中未引用您的类SearchResultDto
。尝试添加@XmlSeeAlso(ProfileDto.class)
到SearchResultDto
.
回答by Slava
I had the same exception on Tomcat.. I found another problem - when i use wsimport over maven plugin to generate stubs for more then 1 WSDLs - class ObjectFactory
(stubs references to this class) contains methods ONLY for one wsdl. So you should merge all methods in one ObjectFactory
class (for each WSDL) or generate each wsdl stubs in different directories (there will be separates ObjectFactory
classes). It solves problem for me with this exception..J
我在 Tomcat 上遇到了同样的异常ObjectFactory
。因此,您应该将所有方法合并到一个ObjectFactory
类中(对于每个 WSDL)或在不同目录中生成每个 wsdl 存根(会有单独的ObjectFactory
类)。它为我解决了这个例外的问题..J
回答by user64141
I had this error because I registered the wrong class in this line of code:
我有这个错误,因为我在这行代码中注册了错误的类:
JAXBContext context = JAXBContext.newInstance(MyRootXmlClass.class);
回答by Jér?me Verstrynge
This error message happens either because your ProfileDto
class is not registered in the JAXB Content, or the class using it does not use @XmlSeeAlso(ProfileDto.class)
to make processable by JAXB.
出现此错误消息是因为您的ProfileDto
类未在 JAXB 内容中注册,或者使用它的类未用于@XmlSeeAlso(ProfileDto.class)
使 JAXB 可处理。
About your comment:
关于您的评论:
I was under the impression the annotations was only needed when the referenced class was a sub-class.
我的印象是只有当引用的类是子类时才需要注释。
No, they are also needed when not declared in the JAXB context or, for example, when the only class having a static reference to it has this reference annotated with @XmlTransient
. I maintain a tutorial here.
不,当未在 JAXB 上下文中声明时,或者例如,当唯一具有静态引用的类将此引用注释为@XmlTransient
. 我在这里维护一个教程。
回答by Monica
Fixed it by setting the class name to the property "classesToBeBound" of the JAXB marshaller:
通过将类名设置为 JAXB 编组器的属性“classesToBeBound”来修复它:
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>myclass</value>
</list>
</property>
</bean>
回答by hacker13ua
I had the same problem with spring boot. It resolved when i set package to marshaller.
我对弹簧靴有同样的问题。当我将包设置为编组器时它解决了。
@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;
}