gpt4 book ai didi

java - 对象必须在其@XmlValue 字段中有一些值

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:49 25 4
gpt4 key购买 nike

我有以下代码

@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 作为空元素输出。

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

#4 - inHouseMail 属性已设置且 UdtTextType 具有 null

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue(null);

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

问题是这个 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

关于java - 对象必须在其@XmlValue 字段中有一些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28411016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com