Java 对象的@XmlValue字段中必须有一些值
我有以下代码Java 对象的@XmlValue字段中必须有一些值,java,jaxb,Java,Jaxb,我有以下代码 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "udt_TextType", propOrder = { "value" }) @XmlSeeAlso({ RoadTypeCodeTypeType.class }) public class UdtTextType { @XmlValue @XmlJavaTypeAdapter(NormalizedStringAdapter.class) @XmlSchemaType(
@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
在XML表示中,inHouseMail
字段应为null
my,而不包括结果XML中的该元素
#2-inHouseMail
属性已设置,并且UdtTextType
具有一个值
由于设置了inHouseMail
字段,我们得到了inHouseMail
元素,并且由于填充了它的值
字段,因此也输出了该元素
你好,世界
#3-inHouseMail
属性已设置,并且UdtTextType
具有空字符串值
由于设置了inHouseMail
字段,我们得到了inHouseMail
元素,并且由于其值
字段填充为空字符串
,作为空元素输出
#4-inHouseMail
属性已设置,并且UdtTextType
具有null
值
问题是这个XML应该是什么样子。它不能是像#3这样的空元素,因为JAXB(和XML)不将null表示为空元素。它也不能表示为缺少的InHouseMail
元素,因为这将与场景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
谢谢你,布莱斯,我改了名字。这是堆栈:
code
com.sun.xml.internal.bind.api.AccessorException:Object的@XmlValue字段中必须有一些值:fr.company.jaxb。UdtTextType@17f2dd8]位于fr.company.Service.launchXML(Service.java:303)处,位于fr.company.Service.launchZIP(Service.java:370)处,位于fr.company.MainTest.testGenerationZip(MainTest.java:17)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57))code
我认为这是因为我在adrescompletetype中有一些为null的@xmlement。我该怎么做才能使JAXB不考虑它们呢?好吧,现在我更明白了。我添加了一些测试来检查UdtTextType是否有空值,并且是否有效。非常感谢。
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("Hello World");
AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("");
AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);
UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue(null);
AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);
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