【问题标题】:unable to marshal type [...] as an element because it is missing an @XmlRootElement annotation无法将类型 [...] 编组为元素,因为它缺少 @XmlRootElement 注释
【发布时间】:2018-05-27 11:39:02
【问题描述】:

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

但是..我有这个错误

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 更长更复杂,但如果我能够让这部分工作,我可能会继续剩下的部分)

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    正如错误所说,您需要在类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;
        }
    }
    

    【讨论】:

    • 该死的,我不敢相信它这么容易。我确信我在测试时做到了,可能其他一些代码被破坏了,然后我忘了重新应用相同的代码。谢谢先生!
    【解决方案2】:

    这是旧的,但以防万一有人遇到它,它在这里得到了回答: https://stackoverflow.com/a/59249216/5835746

    基本上,您不应直接使用自动生成的类,而应使用插件自动生成的 ObjectFactory 中的方法来创建这些类的实例。

    编辑自动生成的类,添加@XmlRootElement 注解可能不是一个好主意。

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 2016-08-31
      • 1970-01-01
      • 2014-08-22
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2019-09-07
      相关资源
      最近更新 更多