MOXy @XmlPath被忽略
我有一个非常简单的类,有两个字段,String sourceAddress和int port。
我希望它们映射到源/地址和源/端口节点上,而不是jaxb默认的sourceAddress和sourcePort。
所以我使用MOXy @XmlPath注释。
问题在于注释被忽略,我得到了“jaxb default”xml文件:
<szk>
<sourceAddress>test</sourceAddress>
<sourcePort>10000</sourcePort>
</sz>
提前感谢任何帮助阿戈斯蒂诺
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SZK {
@XmlPath("source/address")
private String sourceAddress;
@XmlPath("source/port")
private int sourcePort;
public static void main (String [] args) throws JAXBException{
SZK k = new SZK();
k.sourceAddress = "test";
k.sourcePort = 10000;
javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(SZK.class);
Marshaller 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
了解更多信息:
