java - 无法将类型 [...] 编码(marshal)为元素,因为它缺少 @XmlRootElement 注释

标签 java jaxb

令人难以置信的是,关于这个主题已经有这么多问题了。尽管如此,它们都没有帮助我,因为我了解到这是一个常见错误,并且我相信我的代码是正确的,因为它与我同事传递给我的另一个项目完全相同。

但是..我有这个错误

unable to marshal type "modules.CollaborationInfo" as an element because it is missing an @XmlRootElement annotation

给定此类 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;
    }


}

作为 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"));
}

我应该能够得到输出(OUT.xml):

<?xml version="1.0" encoding="UTF-8"?>
    <CollaborationInfo>
        <AgreementRef pmode="String1"/>
        <ConversationId>String2</ConversationId>
    </CollaborationInfo>

但我就是做不到。 谁能告诉我哪里错了?

(当然,真正的 XML 更长、更复杂,但如果我能够让这部分工作,我就可以继续其余部分)

最佳答案

正如错误所述,您需要在类 CollaborationInfo 前面添加注释 @XmlRootElement。您的 @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;
    }
}

关于java - 无法将类型 [...] 编码(marshal)为元素,因为它缺少 @XmlRootElement 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795438/

相关文章:

java - JAXB 和 XSLT 处理器

java - Tomcat 因 JDK : ZIP_GetEntry2 中的 native 代码而崩溃

c# - 是否有 KeyValuePair<T, T> (即单个 hashmap 'cell' )类型的 Java 等价物?

java - XML 请求 Jaxb

java - JAXB + OSGi 与 Java 11

java - 我应该更改/更改 xjc 生成的 java 文件吗?

java - 可以使用配置文件来描述 JAXB 导出吗?

java - 在java程序中使用android aapt

java - 消费者之间的循环不起作用?

Java 程序无法在 linux 中挂载的系统上写入