我有以下代码

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "udt_TextType", propOrder = { "value" })
@XmlSeeAlso({ RoadTypeCodeTypeType.class })
public class UdtTextType {

@XmlValue
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String value;

/**
 * Gets the value of the value property.
 *
 * @return possible object is {@link String }
 *
 */
public String getValue() {
    return value;
}

/**
 * Sets the value of the value property.
 *
 * @param value
 *            allowed object is {@link String }
 *
 */
public void setValue(String value) {
    this.value = value;
}

当我尝试编码此对象时:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AdresseCompleteType", propOrder = { "inHouseMail", "buildingName", "buildingNumber", "blockName", "roadType",
        "streetName", "postcode", "cityName", "lineFive" })
public class AdresseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;
    @XmlElement(name = "BuildingName")
    protected UdtTextType buildingName;
    @XmlElement(name = "BuildingNumber")
    protected UdtTextType buildingNumber;
    ...

我有以下错误:
对象的@XmlValue字段中必须有一些值

为什么会出现此错误?
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: fr.company.jaxb.UdtTextType@17f2dd8] at
    fr.company.Service.launchXML(Service.java:303) at
    fr.company.Service.launchZIP(Service.java:370) at
    fr.company.MainTest.testGenerationZip(MainTest.java:17) at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57))

谢谢!

最佳答案

以下内容说明了为什么会出现异常:

Java模型

下面是演示该问题的模型的简化版本:

UdtTextType

此类具有一个用@XmlValue注释的字段:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class UdtTextType {

    @XmlValue
    protected String value;

    public void setValue(String value) {
        this.value = value;
    }

}

AdreseCompleteType

要注意的重要一点是inHouseMail字段是一种具有@XmlValue的映射的类型。
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AdreseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;

    public void setInHouseMail(UdtTextType inHouseMail) {
        this.inHouseMail = inHouseMail;
    }

}

XML表示

让我们检查一下对象模型可能产生的不同XML表示形式。

#1-inHouseMail属性为null
AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(null);

在XML表示中,inHouseMail字段应为null,但不将元素包含在生成的XML中。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType/>

#2-设置了inHouseMail属性,并且UdtTextType具有值
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("Hello World");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

由于设置了inHouseMail字段,我们获得了InHouseMail元素,并且由于填充了它的value字段,该字段也被输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail>Hello World</InHouseMail>
</adreseCompleteType>

#3-设置了inHouseMail属性,并且UdtTextType具有空字符串值
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

由于设置了inHouseMail字段,因此我们获得了InHouseMail元素,并且由于其value字段被填充为空String,而该inHouseMail输出为空元素。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail></InHouseMail>
</adreseCompleteType>

#4-设置了UdtTextType属性,并且null具有InHouseMail
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue(null);

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

问题是此XML应该是什么样子。它不能像#3那样是空元素,因为JAXB(和XML)不会将null表示为空元素。也不能将其表示为缺少的ojit_code元素,因为这会与场景#1发生冲突。因此,抛出以下异常:
Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
    at forum28411016.Demo.main(Demo.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:235)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:250)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:347)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:582)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:325)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 8 more
Caused by: com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl$TransducerImpl.writeLeafElement(RuntimeClassInfoImpl.java:395)
    at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:239)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:114)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343)
    ... 12 more

09-12 12:04