9

我有一个复杂的 xml 架构,它是国际标准的一部分,因此我无法更改架构文件,并且我使用 Maven 从它们生成 JAXB 对象。当我尝试将数据设置到我的 JAXB 对象中然后编组根元素时,我在几个复杂类型上得到了缺少的 @XmlRootElement 错误。我的问题是如何解决这个问题?环境:

  • JAXB 2.2.2
  • Maven 3.0.3
  • 日蚀朱诺
  • Java 1.6
  • 视窗 7 64 位

我已经广泛研究了网络和这个网站,但没有找到我的问题的答案,包括这篇文章: http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html

还有这个关于堆栈溢出的问题: JAX-WS MarshalException with custom JAX-B bindings: Unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation

我尝试在自定义绑定文件中向有问题的类添加注释,但是它将注释添加到类以及 getContent() 方法,并且该类无法编译。我认为无论如何我都不应该添加这个注释,因为这是一个复杂的类型,它是根的一个组件,实际上它在树中嵌套得相当深。我已经读到您可以将<xjc:simple/>标签添加到全局绑定元素,但是我已经向全局绑定元素添加了许多属性,并且当我这样做时它不会编译。

有人可以在这里帮助我指出正确的方向吗,这可能是我构建 JAXB 对象的方式中的错误吗?我已经在调试器中完成了它,似乎当 com.sun.xml.bind.v2.runtime.property 包中的 ArrayReferenceNodeProperty 类尝试序列化列表主体并且当它获取这些类的 JaxBeanInfo 时,没有t 一个 dom 处理程序集并且 jaxbType 不是一个对象,因此它认为它需要是一个根。

我没有在基于 Web 的服务中使用它,除了绑定文件中用于解决命名冲突的调整之外,还没有创建任何自定义类或绑定。

这是顶级架构文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xs:annotation>
    <xs:documentation>
        *****************************************************************************
        *                                                                           *
        *                        S1000D Issue 3                                     *
        *                          XML Schema                                       *
        *                                                                           *
        *                     Release Version 3.0                                   *
        *                       Date: 2007-07-31                                    *
        *                                                                           *
        *                                                                           *
        * The following attribute definition on the root element may be used to     *
        * call this set of declarations:-                                           *
        *                                                                           *
        * xsi:noNamespaceSchemaLocation=                                            *
        * "http://www.s1000d.org/S1000D_3-0/xml_schema_master/dm/procedSchema.xsd"  *
        *****************************************************************************
    </xs:documentation>
</xs:annotation>
<xs:redefine schemaLocation="elementGroups.xsd">
    <xs:group name="APPLIC">
        <xs:choice>
            <xs:sequence>
                <xs:element ref="displaytext"/> 
                <xs:choice minOccurs="0">
                    <xs:element ref="assert"/>
                    <xs:element ref="evaluate"/>
                </xs:choice>
            </xs:sequence>
            <xs:choice>
                <xs:element ref="assert"/>
                <xs:element ref="evaluate"/>
            </xs:choice>
        </xs:choice>
    </xs:group>
    <xs:group name="NPAR">
        <xs:sequence>
            <xs:group ref="WC"/>
            <xs:group ref="NPFFT" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="NPFFT">
        <xs:choice>
            <xs:group ref="NP"/>
            <xs:group ref="FFT"/>
        </xs:choice>
    </xs:group>
    <xs:group name="paracon">
        <xs:choice>
            <xs:group ref="text"/>
            <xs:group ref="list"/>
        </xs:choice>
    </xs:group>
    <xs:group name="text">
        <xs:choice>
            <xs:element ref="ein"/>
            <xs:element ref="cb"/>
            <xs:element ref="parasigdata"/>
            <xs:element ref="quantity"/>
            <xs:element ref="xref"/>
            <xs:element ref="indxflag"/>
            <xs:element ref="change"/>
            <xs:element ref="emphasis"/>
            <xs:element ref="symbol"/>
            <xs:element ref="subscrpt"/>
            <xs:element ref="supscrpt"/>
            <xs:element ref="refdm"/>
            <xs:element ref="reftp"/>
            <xs:element ref="ftnote"/>
            <xs:element ref="ftnref"/>
            <xs:element ref="acronym"/>
            <xs:element ref="acroterm"/>
            <xs:element ref="capgrp"/>
        </xs:choice>
    </xs:group>
    <xs:group name="TEXT-NOREFS">
        <xs:choice>
            <xs:element ref="xref"/>
            <xs:element ref="indxflag"/>
            <xs:element ref="symbol"/>
            <xs:element ref="subscrpt"/>
            <xs:element ref="supscrpt"/>
            <xs:element ref="ftnref"/>
            <xs:element ref="acronym"/>
            <xs:element ref="acroterm"/>
        </xs:choice>
    </xs:group>
    <xs:group name="STEP1">
        <xs:sequence>
            <xs:element ref="step1"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP2">
        <xs:sequence>
            <xs:element ref="step2"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP3">
        <xs:sequence>
            <xs:element ref="step3"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP4">
        <xs:sequence>
            <xs:element ref="step4"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP5">
        <xs:sequence>
            <xs:element ref="step5"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP6">
        <xs:sequence>
            <xs:element ref="step6"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP7">
        <xs:sequence>
            <xs:element ref="step7"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP8">
        <xs:sequence>
            <xs:element ref="step8"/>
        </xs:sequence>
    </xs:group>
</xs:redefine>
<xs:redefine schemaLocation="complexTypes.xsd">
    <xs:complexType name="contentType">
        <xs:complexContent>
            <xs:restriction base="contentType">
                <xs:sequence>
                    <xs:element ref="refs" minOccurs="0"/>
                    <xs:element ref="proced"/>
                </xs:sequence>
                <xs:attribute ref="id"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:redefine>
<xs:element name="dmodule" type="dmoduleType"/>

对于外部参照类型,我只设置了两个属性,而对于 refdmType,我只设置了另一种属于 ALLDMC 组的复杂类型。错误的两种复杂类型的代码是:

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" elementFormDefault="qualified" attributeFormDefault="qualified">
    <xs:import namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#" schemaLocation="rdf.xsd"/>
    <xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
    <xs:include schemaLocation="attributeGroups.xsd"/>
    <xs:include schemaLocation="simpleElements.xsd"/>
    <xs:include schemaLocation="complexElements.xsd"/>
    <xs:include schemaLocation="elementGroups.xsd"/>
    <xs:complexType name="refdmType">
        <xs:choice>
            <xs:sequence>
                <xs:element ref="applic" minOccurs="0"/>
                <xs:element ref="dmcextension" minOccurs="0"/>
                <xs:group ref="ALLDMC"/>
                <xs:element ref="issno" minOccurs="0"/>
                <xs:element ref="dmtitle" minOccurs="0"/>
                <xs:element ref="language" minOccurs="0"/>
            </xs:sequence>
            <xs:group ref="xlink:XLINKEXT" minOccurs="0" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:attribute ref="target"/>
        <xs:attribute ref="refapplic"/>
        <xs:attributeGroup ref="bodyatt"/>
        <xs:attributeGroup ref="cntlcontent"/>
        <xs:attributeGroup ref="xlink:XLINKATT"/>
    </xs:complexType>
    <xs:complexType name="xrefType" mixed="true">
        <xs:sequence>
            <xs:element ref="applic" minOccurs="0"/>
            <xs:group ref="SPARCON" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="xrefid"/>
        <xs:attribute ref="xidtype"/>
        <xs:attribute ref="target"/>
        <xs:attribute ref="destitle"/>
        <xs:attribute ref="pretext"/>
        <xs:attribute ref="posttext"/>
        <xs:attribute ref="refapplic"/>
        <xs:attributeGroup ref="cntlcontent"/>
        <xs:attributeGroup ref="xlink:XLINKATT3"/>
    </xs:complexType>
    </xs:schema>

这里是 JAXB 生成类的主要部分。我删除了大部分 getter 和 setter 以节省空间: RefdmType:

/**
 * <p>Java class for refdmType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="refdmType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;choice>
 *         &lt;sequence>
 *           &lt;element ref="{}applic" minOccurs="0"/>
 *           &lt;element ref="{}dmcextension" minOccurs="0"/>
 *           &lt;group ref="{}ALLDMC"/>
 *           &lt;element ref="{}issno" minOccurs="0"/>
 *           &lt;element ref="{}dmtitle" minOccurs="0"/>
 *           &lt;element ref="{}language" minOccurs="0"/>
 *         &lt;/sequence>
 *         &lt;group ref="{http://www.w3.org/1999/xlink}XLINKEXT" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/choice>
 *       &lt;attGroup ref="{http://www.w3.org/1999/xlink}XLINKATT"/>
 *       &lt;attGroup ref="{}cntlcontent"/>
 *       &lt;attGroup ref="{}bodyatt"/>
 *       &lt;attribute ref="{}target"/>
 *       &lt;attribute ref="{}refapplic"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "refdmType", propOrder = {
    "applicAndDmcextensionAndAge"
})
public class RefdmType implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElements({
        @XmlElement(name = "applic", type = ApplicType.class),
        @XmlElement(name = "dmcextension", type = DmcextensionType.class),
        @XmlElement(name = "age", type = AgeType.class),
        @XmlElement(name = "avee", type = AveeType.class),
        @XmlElement(name = "issno", type = IssnoType.class),
        @XmlElement(name = "dmtitle", type = DmtitleType.class),
        @XmlElement(name = "language", type = LanguageType.class),
        @XmlElement(name = "resource", namespace = "http://www.w3.org/1999/xlink", type = Resource.class),
        @XmlElement(name = "locator", namespace = "http://www.w3.org/1999/xlink", type = Locator.class),
        @XmlElement(name = "arc", namespace = "http://www.w3.org/1999/xlink", type = Arc.class)
    })
    protected List<Serializable> applicAndDmcextensionAndAge;
    @XmlAttribute(name = "target")
    protected String target;
    @XmlAttribute(name = "refapplic")
    @XmlIDREF
    @XmlSchemaType(name = "IDREF")
    protected Object refapplic;
    @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String type;
    @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
    protected String href;
    @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink")
    protected String title;
    @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String show;
    @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String actuate;
    @XmlAttribute(name = "authname")
    protected String authname;
    @XmlAttribute(name = "authdoc")
    protected String authdoc;
    @XmlAttribute(name = "id")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;
    @XmlAttribute(name = "level")
    @XmlSchemaType(name = "nonNegativeInteger")
    protected BigInteger level;
    @XmlAttribute(name = "mark")
    @XmlSchemaType(name = "nonNegativeInteger")
    protected BigInteger mark;
    @XmlAttribute(name = "change")
    protected ChangeType change;
    @XmlAttribute(name = "rfc")
    protected String rfc;

    /**
     * Gets the value of the applicAndDmcextensionAndAge property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the applicAndDmcextensionAndAge property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getApplicAndDmcextensionAndAge().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link ApplicType }
     * {@link DmcextensionType }
     * {@link AgeType }
     * {@link AveeType }
     * {@link IssnoType }
     * {@link DmtitleType }
     * {@link LanguageType }
     * {@link Resource }
     * {@link Locator }
     * {@link Arc }
     * 
     * 
     */
    public List<Serializable> getApplicAndDmcextensionAndAge() {
        if (applicAndDmcextensionAndAge == null) {
            applicAndDmcextensionAndAge = new ArrayList<Serializable>();
        }
        return this.applicAndDmcextensionAndAge;
    }
    }

外部参照类型:

 /**
 * <p>Java class for xrefType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within    this class.
 * 
 * <pre>
 * &lt;complexType name="xrefType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}applic" minOccurs="0"/>
 *         &lt;group ref="{}SPARCON" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *       &lt;attGroup ref="{}cntlcontent"/>
 *       &lt;attGroup ref="{http://www.w3.org/1999/xlink}XLINKATT3"/>
 *       &lt;attribute ref="{}xrefid"/>
 *       &lt;attribute ref="{}xidtype"/>
 *       &lt;attribute ref="{}target"/>
 *       &lt;attribute ref="{}destitle"/>
 *       &lt;attribute ref="{}pretext"/>
 *       &lt;attribute ref="{}posttext"/>
 *       &lt;attribute ref="{}refapplic"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "xrefType", propOrder = {
"content"
})
public class XrefType implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElementRefs({
      @XmlElementRef(name = "supscrpt", type = JAXBElement.class),
      @XmlElementRef(name = "subscrpt", type = JAXBElement.class),
      @XmlElementRef(name = "applic", type = JAXBElement.class)
    })
   @XmlMixed
   protected List<Serializable> content;
   @XmlAttribute(name = "xrefid")
   @XmlJavaTypeAdapter(Adapter5 .class)
   @XmlSchemaType(name = "IDREF")
   protected String xrefid;
   @XmlAttribute(name = "xidtype")
   protected XidtypeType xidtype;
   @XmlAttribute(name = "target")
   protected String target;
   @XmlAttribute(name = "destitle")
   protected String destitle;
   @XmlAttribute(name = "pretext")
   protected String pretext;
   @XmlAttribute(name = "posttext")
   protected String posttext;
   @XmlAttribute(name = "refapplic")
   @XmlIDREF
   @XmlSchemaType(name = "IDREF")
   protected Object refapplic;
   @XmlAttribute(name = "authname")
   protected String authname;
   @XmlAttribute(name = "authdoc")
   protected String authdoc;
   @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String type;
   @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
   protected String href;
   @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink")
   protected String title;
   @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String show;
   @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String actuate;

   /**
    * Gets the value of the content property.
    * <p>
    * This accessor method returns a reference to the live list,
    * not a snapshot. Therefore any modification you make to the
    * returned list will be present inside the JAXB object.
    * This is why there is not a <CODE>set</CODE> method for the content property.
    * 
    * <p>
    * For example, to add a new item, do as follows:
    * <pre>
    *    getContent().add(newItem);
    * </pre>
    * 
    * 
    * <p>
    * Objects of the following type(s) are allowed in the list
    * {@link String }
    * {@link JAXBElement }{@code <}{@link String }{@code >}
    * {@link JAXBElement }{@code <}{@link ApplicType }{@code >}
    * {@link JAXBElement }{@code <}{@link String }{@code >}
    * 
    * 
    */
    public List<Serializable> getContent() {
       if (content == null) {
        content = new ArrayList<Serializable>();
       }
       return this.content;
    }
}

最后是我的自定义绑定文件:

    <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">

    <jxb:globalBindings typesafeEnumMemberName="generateName"
        typesafeEnumMaxMembers="1500" collectionType="java.util.ArrayList"
        generateValueClass="true" choiceContentProperty="true">
        <xjc:serializable uid="1" />
    </jxb:globalBindings>

    <jxb:bindings schemaLocation="simpleTypes.xsd" node="/xs:schema">
        <jxb:bindings node="//xs:simpleType[@name='YEAR']">
            <jxb:javaType name="java.lang.Integer" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='DAY']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='MONTH']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='issnovalueType']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="attributes.xsd" node="/xs:schema">
        <jxb:bindings node="xs:attribute[@name='inwork']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
        <jxb:bindings node="//xs:attribute[@name='xrefid']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="complexTypes.xsd" node="/xs:schema">
        <jxb:bindings node="xs:complexType[@name='dmoduleType']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="dmodule" />
            </annox:annotate>
        </jxb:bindings>
        <jxb:bindings node="//xs:complexType[@name='skillType']">
            <jxb:class name="SkillTypeComplex" />
        </jxb:bindings>
        <!--<jxb:bindings node="//xs:complexType[@name='xrefType']">
              <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="xref" />
            </annox:annotate>
            <jxb:bindings node=".//xs:sequence//xs:element[@ref='applic']">
                <jxb:property name="xrefApplic"/>
            </jxb:bindings>
        </jxb:bindings>-->
    </jxb:bindings>

    <jxb:bindings schemaLocation="elementGroups.xsd" node="//xs:group[@name='APPLIC']">
        <jxb:bindings node=".//xs:choice//xs:sequence">
            <jxb:bindings node=".//xs:choice">
                <jxb:property name="assertEvalChoice" />
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="dc.xsd" node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='language']">
            <jxb:class name="DcLanguage" />
        </jxb:bindings>
        <jxb:bindings node="//xs:element[@name='subject']">
            <jxb:class name="DcSubject" />
        </jxb:bindings>
        <jxb:bindings node="//xs:element[@name='title']">
            <jxb:class name="DcTitle" />
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

对于为什么会发生这种情况以及如何解决它的建议的高级视图将不胜感激。我只使用 JAXB 几个月,并不是专家。谢谢你。

这是我看到的错误:

javax.xml.bind.MarshalException - 带有链接异常:[com.sun.istack.SAXException2: 由于缺少@XmlRootElement 注释] 在 com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323) 在 com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:249)在 javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96) 在 com.synesis7.s7exporter.client.s1000d.procedural.S7ProceduralRunnable.writeToXml(S7ProceduralRunnable.java:142) 在 com.synesis7.s7exporter.client .s1000d.procedural.S7ProceduralRunnable.createProceduralDm(S7ProceduralRunnable.java:118) 在 com.synesis7.s7exporter.client.s1000d.procedural.S7ProceduralRunnable。在 com.synesis7.s7exporter.S7ExporterController.exportSchema(S7ExporterController.java:118) 在 com.synesis7.s7exporter.S7exporter 上运行(S7ProceduralRunnable.java:93) .initializeExporter(S7exporter.java:58) at com.synesis7.s7exporter.S7exporter.main(S7exporter.java:38) 原因:com.sun.istack.SAXException2:无法编组类型“com.synesis7.s7exporter.domain。 s1000d.dm.common.XrefType”作为元素,因为它在 com.sun.xml.bind 的 com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:252) 处缺少 @XmlRootElement 注释.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:324) 在 com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty。com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159) 上的 serializeListBody(ArrayReferenceNodeProperty.java:118) com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody( ClassBeanInfoImpl.java:344) 在 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700) 在 com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty. java:69) 在 com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172) 在 com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty. java:159) 在 com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:344) 在 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700) 在 com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69) 在 com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty. java:172) 在 com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159) 在 com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java: 344) 在 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700) 在 com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69)在 com.sun.xml.bind.v2.runtime.property.ArrayERProperty 的 com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172)。com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:344) 上的 serializeBody(ArrayERProperty.java:159) com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer. java:700) 在 com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:156) 在 com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java: 344) 在 com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:156) 的 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)在 com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:344) 在 com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:338) 在 com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:156) 的 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)在 com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700) 在 com.sun.com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:344) .xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:156) 在 com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:161) 在 com.sun。 xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:193) 在 com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:321) 在 com.sun.xml.bind .v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:328) 在 com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76) 在 com.sun.xml.bind.v2.runtime。 com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320) 上的 XMLSerializer.childAsRoot(XMLSerializer.java:498) ... 还有 9 个

4

2 回答 2

20

您可以将对象包装在 的实例中JAXBElement以提供根元素信息。ObjectFactory如果您从 XML 模式生成模型,则在生成的类上有方便的方法。

了解更多信息

于 2012-11-26T23:34:40.937 回答
0

您可以使用ObjectFactory类来解决没有@XmlRootElement的类。ObjectFactory 具有将其包装在 JAXBElement 周围的重载方法。 Method:1执行对象的简单创建,Method:2将使用@JAXBElement包装对象。始终使用Method:2来避免 javax.xml.bind.MarshalException - 链接异常缺少 @XmlRootElement 注释

方法:1

public GetCountry createGetCountry() {
        return new GetCountry();
    }

方法:2

 @XmlElementDecl(namespace = "my/name/space", name = "getCountry")
 public JAXBElement<GetCountry> createGetCountry(GetCountry value) {
        return new JAXBElement<GetCountry>(_GetCountry_QNAME, GetCountry.class, null, value);
    }

希望这会有所帮助...

于 2018-03-03T12:23:13.097 回答