我有一个非常简单的类,有两个字段,String sourceAddress 和 int port。
我希望它们映射到源/地址和源/端口节点而不是
jaxb 默认的 sourceAddress 和 sourcePort。
所以我使用 MOXy @XmlPath 注释。
问题是注释被忽略了,我得到了“jaxb default”xml文件:
<szk>
<sourceAddress>test</sourceAddress>
<sourcePort>10000</sourcePort>
</sz>
提前感谢您的帮助
阿戈斯蒂诺
导入 javax.xml.bind.*;
导入 javax.xml.bind.annotation.*;
导入 org.eclipse.persistence.jaxb.JAXBContext;
导入 org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共(public)课 SZK {
@XmlPath("源/地址")
私有(private)字符串源地址;
@XmlPath("源/端口")
私有(private) int 源端口;
公共(public)静态 void main (String [] args) 抛出 JAXBException{
SZK k = 新 SZK();
k.sourceAddress = "测试";
k.sourcePort = 10000;
javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(SZK.class);
编码器 m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(k, System.out);
}
}
最佳答案
此问题的最可能原因是您缺少 jaxb.properties
文件以指定 EclipseLink MOXy应该用作 JAXB 提供程序。 jaxb.properties
文件必须与您的域模型放在同一个包中,并包含以下条目:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
欲了解更多信息:
关于jaxb - MOXy @XmlPath 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948924/