jaxb - MOXy @XmlPath 被忽略

标签 jaxb eclipselink moxy

我有一个非常简单的类,有两个字段,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

欲了解更多信息:
  • http://bdoughan.blogspot.com/2011/05/specifying-eclipselink-moxy-as-your.html
  • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
  • http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
  • 关于jaxb - MOXy @XmlPath 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948924/

    相关文章:

    java - 为 MOXy JSON 编码重命名元素名称

    java - Jersey 休息服务未返回 XML 响应

    javascript - 在 jquery 中访问 Spring MVC 模型对象

    java - MOXY 中的 JAXB 继承

    java - 如何从 JPA 中的持久性异常中获取不同类型的错误?

    java - 将 Moxy 设置为默认 jaxb 实现提供者

    java - 通过 moxy 在字段名称中使用斜杠将 POJO 转换为 JSON

    java - xsd: 自定义类型的列表生成到 List<String>

    spring - 如何使用 JPA QueryBuilder 提供自定义表达式或谓词

    java - ManyToMany 和 JPA EclipseLink 的问题