Moxy is marshalling unmapped java properties

落爺英雄遲暮 提交于 2020-01-04 07:25:07

问题


I was able to use the XML as external meta-data by following the article here. However, Moxy is marshalling the properties that are neither annotated nor specified in the external XML meta-data. Below is the e.g. How to avoid this behavior? I tried using xml-mapping-metadata-complete="true" but it didn't help.

Class with new prefix property added (removed other properties for brevity)

public class Customer
{
    private String prefix;

    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
    }

    public String getPrefix()
    {
        return prefix;
    }
}

meta-data xml

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="blog.bindingfile">
   <xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
   <java-types>
      <java-type name="Customer">
         <xml-root-element />
         <xml-type prop-order="firstName lastName address phoneNumbers" />
         <java-attributes>
            <xml-element java-attribute="firstName" name="first-name" />
            <xml-element java-attribute="lastName" name="last-name" />
            <xml-element java-attribute="phoneNumbers" name="phone-number" />
         </java-attributes>
      </java-type>
      <java-type name="PhoneNumber">
         <java-attributes>
            <xml-attribute java-attribute="type" />
            <xml-value java-attribute="number" />
         </java-attributes>
      </java-type>
   </java-types>
</xml-bindings>

output

<customer xmlns="http://www.example.com/customer">
   <first-name>Jane</first-name>
   <last-name>Doe</last-name>
   <address>
      <street>123 A Street</street>
   </address>
   <phone-number type="work">555-1111</phone-number>
   <phone-number type="cell">555-2222</phone-number>
   <prefix>pre</prefix>
</customer>

回答1:


To omit the prefix property from your marshalled XML, you should declare it as transient in your bindings file:

  ...
  <java-type name="Customer">
     <xml-root-element />
     <xml-type prop-order="firstName lastName address phoneNumbers" />
     <java-attributes>
        <xml-element java-attribute="firstName" name="first-name" />
        <xml-element java-attribute="lastName" name="last-name" />
        <xml-element java-attribute="phoneNumbers" name="phone-number" />
        <xml-transient java-attribute="prefix" />
     </java-attributes>
  </java-type>
  ...

By default, JAXB will map any public fields, so since there was no explicit "annotation" on the prefix field, it is mapped in the default way.

xml-mapping-metadata-complete="true" means "Ignore any annotations found in the Java class and use this bindings file as the sole source of mapping information -- do not augment any existing annotations."

Hope this helps, Rick



来源:https://stackoverflow.com/questions/11849027/moxy-is-marshalling-unmapped-java-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!