MOXy 的 xml 瞬态声明如何工作?
我在一个单独的项目中有一组 bean,我无法更改。这些 bean 具有 JPA 和 JAXB 注释,并在 RESTful 实现中使用。我的大多数关系都是延迟加载的,我希望能够对哪些元素实际编组进行传输实现更精细的控制。
下面是修改后的 MOXy Customer.java 类。
@javax.xml.bind.annotation.XmlType
@javax.xml.bind.annotation.XmlAccessorType(value=javax.xml.bind.annotation.XmlAccessType.PROPERTY)
public class Customer {
private String name;
private Address address;
private List<PhoneNumber> phoneNumbers;
// getters and setters
}
我希望能够使用 MOXy eclipselink-oxm 映射来控制编组内容,但它的行为并不符合我的预期。使用 JAXB 注释,您可以将元素(字段或属性)声明为瞬态元素,但 eclipselink-oxm.xml 只允许对类型进行瞬态声明。但是,当我像这样声明瞬态类型时,会出现以下异常:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
<java-types>
<java-type name="example.gettingstarted.Customer">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="name" xml-path="personal-info/name/text()"/>
<xml-element java-attribute="address" xml-path="contact-info/address"/>
</java-attributes>
</java-type>
<java-type name="example.gettingstarted.PhoneNumber" xml-transient="true" />
</java-types>
</xml-bindings>
异常:
Exception [EclipseLink-110] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [example.gettingstarted.PhoneNumber].
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[phoneNumbers]
Descriptor: XMLDescriptor(example.gettingstarted.Customer --> [DatabaseTable(customer)])
如果我删除 xml-transient 属性或将其设置为 false,则 Customer 将按预期转换为 XML。有什么方法可以在不修改 Customer bean 的情况下抑制电话号码的编组吗?
I have a set of a beans in a separate project that I'm unable to alter. These beans have both JPA and JAXB annotations, and are being used in a RESTful implementation. Most of my relationships are lazy-loaded, and I was hoping to achieve some more granular control over which elements are actually marshalled for transport.
I've got the modified MOXy Customer.java class below.
@javax.xml.bind.annotation.XmlType
@javax.xml.bind.annotation.XmlAccessorType(value=javax.xml.bind.annotation.XmlAccessType.PROPERTY)
public class Customer {
private String name;
private Address address;
private List<PhoneNumber> phoneNumbers;
// getters and setters
}
I was hoping I'd be able to use the MOXy eclipselink-oxm mapping to control what gets marshalled, but it isn't behaving as I'd expect. Using JAXB annotations, you declare an element (field or property) to be transient, but the eclipselink-oxm.xml only allows transient declarations on types. However, when I declare a type transient like so, I get the following exceptions:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
<java-types>
<java-type name="example.gettingstarted.Customer">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="name" xml-path="personal-info/name/text()"/>
<xml-element java-attribute="address" xml-path="contact-info/address"/>
</java-attributes>
</java-type>
<java-type name="example.gettingstarted.PhoneNumber" xml-transient="true" />
</java-types>
</xml-bindings>
Exception:
Exception [EclipseLink-110] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [example.gettingstarted.PhoneNumber].
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[phoneNumbers]
Descriptor: XMLDescriptor(example.gettingstarted.Customer --> [DatabaseTable(customer)])
If I remove the xml-transient attribute, or set it to false, the Customer is transformed into XML as expected. Is there any way I can suppress the marshalling of phone numbers without modifying the Customer bean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以指定使用以下映射文件使 Customer 上的“phoneNumbers”属性变为瞬态:
有关 MOXy 的 XML 映射文件的更多信息,请参阅:
You can specify use the following mapping file to make the "phoneNumbers" property on Customer transient:
For more information on MOXy's XML mapping file see: