Java 与MOXy不一致的解组
我使用MOXy来解组一个相对简单的XML文档,但是我得到了不一致的行为。该文件由两个基本信息(姓名和日期)和记录列表组成。问题是每次都正确地解组了名称和日期,但是我经常没有得到任何记录(这已经通过使用相同的静态数据反复进行单元测试进行了验证,但通过和失败都没有观察到可预测性)。所有的日志记录和断言都验证了一切看起来都很好,只是有时候我得到了全部6条记录,有时候我得到了0(从来没有任何其他数字)。这种前后矛盾的行为对我来说毫无意义,有什么想法吗?我是否缺少注释或其他可以确保正确解组的内容 我尝试了几个不同版本的eclipselink库,结果都是一样的(目前我正在从eclipselink maven存储库加载2.2.0) 报告类Java 与MOXy不一致的解组,java,jaxb,eclipselink,unmarshalling,moxy,Java,Jaxb,Eclipselink,Unmarshalling,Moxy,我使用MOXy来解组一个相对简单的XML文档,但是我得到了不一致的行为。该文件由两个基本信息(姓名和日期)和记录列表组成。问题是每次都正确地解组了名称和日期,但是我经常没有得到任何记录(这已经通过使用相同的静态数据反复进行单元测试进行了验证,但通过和失败都没有观察到可预测性)。所有的日志记录和断言都验证了一切看起来都很好,只是有时候我得到了全部6条记录,有时候我得到了0(从来没有任何其他数字)。这种前后矛盾的行为对我来说毫无意义,有什么想法吗?我是否缺少注释或其他可以确保正确解组的内容 我尝试了
package com.company.report_parser.model;
import org.eclipse.persistence.oxm.annotations.XmlPath;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name="feedback")
public class SpecificAggregateReport extends AggregateReport {
@XmlPath("submitter/org_name/text()")
protected void setPartnerName(String partnerName) {
super.setPartnerName(partnerName);
}
@XmlPath("date_range/end/text()")
protected void setDate(String date) {
super.setDate(date);
}
public List<AggregateRecord> getRecords() {
return super.getRecords();
}
@XmlPath("authstats/record")
@XmlElement(type=SpecificAggregateRecord.class) // This is the element class which implements the AggregateRecord Interface used in the List
protected void setRecords(List<AggregateRecord> records) {
super.setRecords(records);
}
}
样本数据
<?xml version="1.0" encoding="UTF-8" ?>
<feedback>
<submitter>
<org_name>specific</org_name>
<email>support@specific.com</email>
</submitter>
<date_range>
<begin>20110511</begin>
<end>20110511</end>
</date_range>
<report_id>682417472261065178</report_id>
<authstats>
<record>
<row source_ip="184.106.220.108" count="8" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="neutral" d="test.net" />
<spf domain="test.net" identity="spf_envelope_from" result="pass" />
</auth_results>
</record>
<record>
<row source_ip="50.56.76.41" count="6" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="neutral" d="test.net" />
<spf domain="test.net" identity="spf_envelope_from" result="softfail" />
</auth_results>
</record>
<record>
<row source_ip="50.56.76.41" count="6" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="none" d="" />
<spf domain="test.net" identity="spf_envelope_from" result="softfail" />
</auth_results>
</record>
<record>
<row source_ip="184.106.220.108" count="6" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="pass" d="test.net" />
<spf domain="test.net" identity="spf_envelope_from" result="pass" />
</auth_results>
</record>
<record>
<row source_ip="50.56.76.41" count="8" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="pass" d="test.net" />
<spf domain="test.net" identity="spf_envelope_from" result="softfail" />
</auth_results>
</record>
<record>
<row source_ip="184.106.220.108" count="6" policy_domain="test.net" policy="none" action_taken="none" />
<identities envelope_from="test.net" header_from="test.net" />
<auth_results>
<dkim result="none" d="" />
<spf domain="test.net" identity="spf_envelope_from" result="pass" />
</auth_results>
</record>
</authstats>
</feedback>
具体的
support@specific.com
20110511
20110511
682417472261065178
问题
不一致的行为是由于在子类中重写了属性。MOXy考虑要映射的父类和子类中的属性,并且取决于您看到的一种行为或另一种行为的处理顺序
解决方案#1
您需要告诉MOXy父属性未映射,可以通过在聚合记录
和聚合报告
类上指定@xmlacessortype(xmlacesstype.NONE)
来实现:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.NONE)
public class AggregateRecord {
...
}
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SpecificAggregateRecord extends AggregateRecord {
然后在SpecificAggregateRecord
和SpecificAggregateReport
类上指定@XmlAccessorType(XmlAccessType.PROPERTY)
:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.NONE)
public class AggregateRecord {
...
}
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SpecificAggregateRecord extends AggregateRecord {
解决方案#2
如果您只在子类上重写访问器,仅仅是为了应用映射,那么您可能需要考虑使用MOXY的外部映射文件来代替。以下是一个例子:
聚合记录
和聚合报告
类上指定@xmlacessortype(xmlacesstype.NONE)
来实现:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.NONE)
public class AggregateRecord {
...
}
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SpecificAggregateRecord extends AggregateRecord {
然后在SpecificAggregateRecord
和SpecificAggregateReport
类上指定@XmlAccessorType(XmlAccessType.PROPERTY)
:
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.NONE)
public class AggregateRecord {
...
}
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SpecificAggregateRecord extends AggregateRecord {
解决方案#2
如果您只在子类上重写访问器,仅仅是为了应用映射,那么您可能需要考虑使用MOXY的外部映射文件来代替。以下是一个例子:
如果您对探索此选项感兴趣,请告诉我。我终于回到了这个问题,尽管我必须指定
xmlacesstype.FIELD
而不是属性
,它现在似乎一直在工作。我终于回到了这个问题,尽管我必须指定xmlacesstype.FIELD
而不是属性
,但它现在似乎一直在工作。