java – 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的情况下抑制电话号码的编组?
解决方法:
您可以指定使用以下映射文件在Customer瞬态上创建“phoneNumbers”属性:
<?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" />
<xml-transient java-attribute="phoneNumbers"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
有关MOXy的XML映射文件的更多信息,请参阅:
> http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
标签:java,eclipselink,jaxb,moxy 来源: https://codeday.me/bug/20190704/1381674.html