[英]unable to marshal type […] as an element because it is missing an @XmlRootElement annotation
Unbelievable how many questions there are already about this subject.令人难以置信的是,关于这个主题已经有多少问题了。 Still, none of them helps me because what I understood is that it is a common error and I believe my code is correct because it is exactly the same as the one of another project my colleague passed me.
尽管如此,它们都没有帮助我,因为我明白这是一个常见的错误,我相信我的代码是正确的,因为它与我同事传递给我的另一个项目完全相同。
But.. I have this error但是..我有这个错误
unable to marshal type "modules.CollaborationInfo" as an element because it is missing an @XmlRootElement annotation
Given this class CollaborationInfo:鉴于此类 CollaborationInfo:
package modules;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CollaborationInfo", propOrder = {
"AgreementRef",
"ConversationId"
})
public class CollaborationInfo {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="AgreementRef")
public static class AgreementRef {
@XmlAttribute
private String pmode;
public String getPmode() {
return pmode;
}
public void setPmode(String pmode) {
this.pmode = pmode;
}
}
@XmlElementRef(name = "AgreementRef")
protected AgreementRef AgreementRef = new AgreementRef();
@XmlElement(name="ConversationId")
protected String ConversationId;
public String getPmode() {
return AgreementRef.getPmode();
}
public void setPmode(String value) {
this.AgreementRef.setPmode(value);
}
public String getConversationId() {
return ConversationId;
}
public void setConversationId(String value) {
this.ConversationId = value;
}
}
And as main():而作为主():
public static void main(String[] args) throws Exception{
JAXBContext contextObj = JAXBContext.newInstance(CollaborationInfo.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
CollaborationInfo CI = new CollaborationInfo();
CI.setPmode("String1");
CI.setConversationId("String2");
marshallerObj.marshal(CI, new FileOutputStream("C:\\OUT.xml"));
}
I should be able to have as output (OUT.xml):我应该能够作为输出(OUT.xml):
<?xml version="1.0" encoding="UTF-8"?>
<CollaborationInfo>
<AgreementRef pmode="String1"/>
<ConversationId>String2</ConversationId>
</CollaborationInfo>
But I just can't.但我就是做不到。 Can someone tell me where I am wrong?
有人能告诉我我错在哪里吗?
(Of course the real XML is much more long and complex, but probably if I'm able to have this part working I'm able to continue the rest) (当然,真正的 XML 更长更复杂,但可能如果我能够让这部分工作,我就可以继续其余部分)
As the errors says, you need to add the annotation @XmlRootElement
in front of the class CollaborationInfo
.正如错误所说,您需要在类
CollaborationInfo
前面添加注释@XmlRootElement
。 Your @XmlRootElement
is for the static class AgreementRef
.您的
@XmlRootElement
用于静态类AgreementRef
。
package modules;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CollaborationInfo", propOrder = {
"AgreementRef",
"ConversationId"
})
public class CollaborationInfo {
@XmlAccessorType(XmlAccessType.FIELD)
public static class AgreementRef {
@XmlAttribute
private String pmode;
public String getPmode() {
return pmode;
}
public void setPmode(String pmode) {
this.pmode = pmode;
}
}
@XmlElement(name = "AgreementRef")
protected AgreementRef AgreementRef = new AgreementRef();
@XmlElement(name="ConversationId")
protected String ConversationId;
public String getPmode() {
return AgreementRef.getPmode();
}
public void setPmode(String value) {
this.AgreementRef.setPmode(value);
}
public String getConversationId() {
return ConversationId;
}
public void setConversationId(String value) {
this.ConversationId = value;
}
}
This is old but just in case someone runs into it, it was answered here: https://stackoverflow.com/a/59249216/5835746这是旧的,但以防万一有人遇到它,在这里回答: https : //stackoverflow.com/a/59249216/5835746
Basically, you should not use the auto-generated classes directly but use the methods from ObjectFactory , which was also auto-generated by the plugin, to create instances of those classes.基本上,您不应该直接使用自动生成的类,而是使用ObjectFactory 中的方法(也是由插件自动生成的)来创建这些类的实例。
Editing the auto-generated classes, to add the @XmlRootElement annotation is probably not a good idea.编辑自动生成的类,添加 @XmlRootElement 注释可能不是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.