Moxy正在编组未映射的java属性 [英] Moxy is marshalling unmapped java properties
问题描述
我可以通过这里的文章。但是,Moxy正在编组在外部XML 元数据中既未注释也未指定的属性。以下是例如如何避免这种行为?我尝试使用 xml-mapping-metadata-complete =true
但它没有帮助。
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.
添加了新前缀属性的类(为简洁起见,删除了其他属性)
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的/ code>属性,您应该在绑定文件中将其声明为
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将映射任何公共字段,所以既然有在前缀
字段中没有明确的注释,它以默认方式映射。
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
表示忽略在Java类中找到的任何注释,并使用此绑定文件作为映射信息的唯一来源 - 不要增加任何现有注释。
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."
希望这会有所帮助,
Rick
Hope this helps, Rick
这篇关于Moxy正在编组未映射的java属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!