2012-08-07 40 views
2

我能够通过遵循article here将XML用作外部元数据。但是,Moxy正在编组属性,这些属性既未注释也未在外部XML元数据中指定。下面是例如如何避免这种行为?我尝试使用xml-mapping-metadata-complete="true",但它没有帮助。Moxy编组未映射的java属性

类添加了新的前缀属性(已删除其他属性为简洁起见)

public class Customer 
{ 
    private String prefix; 

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

    public String getPrefix() 
    { 
     return prefix; 
    } 
} 

元数据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> 

输出

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

回答

2

要从编组XML省略prefix属性,则应宣告它作为您的绑定transient文件:

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

默认情况下,JAXB将映射任何公共领域,所以,因为没有明确的“注解”上在prefix字段中,它以默认方式映射。

xml-mapping-metadata-complete="true"表示“忽略在Java类中找到的任何注释,并将此绑定文件用作映射信息的唯一来源 - 不要增加任何现有的注释。”

希望这会有所帮助, Rick

+0

Thankx。仍然试图得到“例外配置;-)”。你还可以看看这是否是一个错误http://stackoverflow.com/questions/11853821/moxy-not-honoring-the-super-class-interface-properties – 2012-08-07 20:46:54