我能够按照 article here 使用 XML 作为外部元数据。然而,Moxy 正在编码外部 XML 元数据中既没有注释也没有指定的属性。下面是例如如何避免这种行为?我尝试使用 xml-mapping-metadata-complete="true"
但没有帮助。
添加了新前缀属性的类(为了简洁删除了其他属性)
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>
最佳答案
要从编码 XML 中省略 prefix
属性,您应该在绑定(bind)文件中将其声明为 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 将映射任何公共(public)字段,因此由于 prefix
字段上没有显式“注释”,因此它以默认方式映射。
xml-mapping-metadata-complete="true"
表示“忽略在 Java 类中找到的任何注释并使用此绑定(bind)文件作为映射信息的唯一来源——不增加任何现有的注释。”
希望这有帮助, 瑞克
关于jaxb - Moxy 正在编码未映射的 java 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11849027/