MOXy 的 xml 瞬态声明如何工作?

发布于 2024-10-27 10:56:45 字数 1841 浏览 1 评论 0原文

我在一个单独的项目中有一组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-11-03 10:56:45

您可以指定使用以下映射文件使 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 映射文件的更多信息,请参阅:

You can specify use the following mapping file to make the "phoneNumbers" property on Customer transient:

<?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>

For more information on MOXy's XML mapping file see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文