JAXB/MOXY映射问题:属性或字段phoneType必须是属性,因为另一个字段或属性用XmlValue注释
下面是我的类和OXM映射。如果我希望phoneType是phone下的子元素,不确定为什么会出现以下异常JAXB/MOXY映射问题:属性或字段phoneType必须是属性,因为另一个字段或属性用XmlValue注释,jaxb,jaxb2,moxy,Jaxb,Jaxb2,Moxy,下面是我的类和OXM映射。如果我希望phoneType是phone下的子元素,不确定为什么会出现以下异常 public class PhoneNumber { public enum PhoneType { Cell, Home }; private PhoneType phoneType; private String type; private String number; public String getTy
public class PhoneNumber
{
public enum PhoneType
{
Cell, Home
};
private PhoneType phoneType;
private String type;
private String number;
public String getType()
{
return type;
}
public void setType(final String type)
{
this.type = type;
}
public String getNumber()
{
return number;
}
public void setNumber(final String number)
{
this.number = number;
}
public void setPhoneType(final PhoneType phoneType)
{
this.phoneType = phoneType;
}
public PhoneType getPhoneType()
{
return phoneType;
}
}
OXM
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
version="2.4" package-name="blog.bindingfile" xml-mapping-metadata-complete="true">
<xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
<java-types>
<java-type name="Customer">
<xml-root-element name="customer"/>
<xml-type prop-order="lastName firstName address phoneNumbers" />
<java-attributes>
<xml-element java-attribute="firstName" name="first-name" />
<xml-element java-attribute="lastName" name="last-name" />
<xml-element java-attribute="phoneNumbers" name="phone-number" />
</java-attributes>
</java-type>
<java-type name="BaseCustomer" xml-transient="true">
<xml-root-element/>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" />
<xml-value java-attribute="number" />
<xml-element java-attribute="phoneType" name="phone-type"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
例外情况
Caused by: Exception [EclipseLink-50010] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The property or field phoneType must be an attribute because another field or property is annotated with XmlValue.
at org.eclipse.persistence.exceptions.JAXBException.propertyOrFieldShouldBeAnAttribute(JAXBException.java:246)
at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:1011)
at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:412)
at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:102)
at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:768)
... 11 more
原因:异常[EclipseLink-50010](Eclipse持久性服务-2.4.0.v20120608-r11652):org.Eclipse.Persistence.exceptions.jaxbeException
异常描述:属性或字段phoneType必须是属性,因为另一个字段或属性用XmlValue注释。
位于org.eclipse.persistence.exception.JAXBException.propertyOrFieldShouldBeAnAttribute(JAXBException.java:246)
位于org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:1011)
位于org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:412)
位于org.eclipse.persistence.jaxb.compiler.Generator(Generator.java:102)
位于org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:768)
... 还有11个
更新
为了完全理解为什么会出现异常,我们将看一个格式化的XML片段。问题在于@XmlValue映射对应于哪个文本片段(第一个、第二个,都不是?)
由于OXM文件中的以下内容,您将收到异常:
异常情况表明,如果类中的另一个字段/属性映射为
@XmlValue
您需要做些什么
您实际上是在尝试映射具有混合内容的元素。可以使用下面的映射文件执行此操作:
完整示例
oxm.xml
演示
用于UM11988974的包;
导入java.io.File;
导入java.util.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
公开课演示{
公共静态void main(字符串[]args)引发异常{
映射属性=新的HashMap();
put(JAXBContextProperties.OXM_元数据_源,“forum11988974/OXM.xml”);
JAXBContext jc=JAXBContext.newInstance(新类[]{PhoneNumber.Class},属性);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
文件xml=新文件(“src/forum11988974/input.xml”);
PhoneNumber PhoneNumber=(PhoneNumber)解组器。解组器(xml);
Marshaller=jc.createMarshaller();
marshaller.marshall(电话号码,系统输出);
}
}
input.xml/Output
555-1111单元
更新
为了完全理解为什么会出现异常,我们将看一个格式化的XML片段。问题在于@XmlValue映射对应于哪个文本片段(第一个、第二个,都不是?)
由于OXM文件中的以下内容,您将收到异常:
异常情况表明,如果类中的另一个字段/属性映射为
@XmlValue
您需要做些什么
您实际上是在尝试映射具有混合内容的元素。可以使用下面的映射文件执行此操作:
完整示例
oxm.xml
演示
用于UM11988974的包;
导入java.io.File;
导入java.util.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
公开课演示{
公共静态void main(字符串[]args)引发异常{
映射属性=新的HashMap();
put(JAXBContextProperties.OXM_元数据_源,“forum11988974/OXM.xml”);
JAXBContext jc=JAXBContext.newInstance(新类[]{PhoneNumber.Class},属性);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
文件xml=新文件(“src/forum11988974/input.xml”);
PhoneNumber PhoneNumber=(PhoneNumber)解组器。解组器(xml);
Marshaller=jc.createMarshaller();
marshaller.marshall(电话号码,系统输出);
}
}
input.xml/Output
555-1111单元
“根据例外情况,如果类中的另一个字段/属性映射为@XmlValue,则不允许将字段/属性映射为@XmlElement”-您可以解释为什么会有此限制吗?如果我们将“电话号码”定义为xml值,因为XSD的简单类型或simpleContent不能有子元素,我理解这是一个问题。我一直忘了@XmlValue的用途。“例外情况表明,如果类中的另一个字段/属性映射为@XmlValue,则不允许将字段/属性映射为@XmlElement”-您能解释为什么会有此限制吗?如果我们将“电话号码”定义为xml值,因为XSD的简单类型或simpleContent不能有子元素,我理解这是一个问题。我一直忘了@XmlValue的用途。
<phone-number type="TYPE">
<phone-type>Cell</phone-type>
</phone-number>
<phone-number type="TYPE">
555-1111
<phone-type>Cell</phone-type>
</phone-number>