对于从替换组生成的抽象超类,我似乎无法正确理解所有 JAXB 魔法。我正在使用 JAXB 的参考实现。
这是类层次结构:
+ DatasourceAbstract
+----- DatasourceQuery
+------DatasourceStatic
鉴于(如下文所述)我已经明确地向 JAXBContext 提供了该类及其所有子类,为什么它们“不为人知”对我来说是个谜。
如果需要更多信息,请告诉我。我提供了我认为相关的所有内容,但不想让帖子超载。
错误(为清楚起见进行了一些编辑):
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Invalid @XmlElementRef : Type "class com.escholar.reports.reportXML.DatasourceAbstract" or any of its subclasses are not known to this context.
this problem is related to the following location:
at protected java.util.LinkedList com.escholar.reports.reportXML.Data.dataSourceSubstitutionGroupHead
at com.escholar.reports.reportXML.Data
at protected com.escholar.reports.reportXML.Data com.escholar.reports.reportXML.ReportJAXB.data
at com.escholar.reports.reportXML.ReportJAXB
at public com.escholar.reports.reportXML.ReportJAXB com.escholar.reports.reportXML.ObjectFactory.createReportResourceJAXB()
at com.escholar.reports.reportXML.ObjectFactory
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
..... Partially ellided stacktrace ............
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.escholar.reports.utilities.reportsamples.ReportXMLGenerator.main(ReportXMLGenerator.java:69)
JAXB上下文创建
这是我创建 JAXB 上下文的地方(触发上述异常的行):
JAXBContext jc =
JAXBContext.newInstance(
com.escholar.reports.reportXML.ObjectFactory.class,
com.escholar.reports.reportXML.DatasourceAbstract.class,
com.escholar.reports.reportXML.DatasourceStatic.class,
com.escholar.reports.reportXML.DatasourceQuery.class);
数据源抽象类
这是它抱怨的课程。请注意,我还使用@XmlSeeAlso 来标识子类。
package com.escholar.reports.jaxb;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DatasourceAbstract")
@XmlSeeAlso({
DatasourceStatic.class,
DatasourceQuery.class
})
public abstract class DatasourceAbstract {
@XmlAttribute(name = "Name")
@XmlSchemaType(name = "anySimpleType")
protected String name;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
}
引用 DatasourceAbstract
这是引用 DatasourceAbstract 的地方:
package com.escholar.reports.jaxb;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"dataSourceSubstitutionGroupHead"
})
@XmlRootElement(name = "Data")
public class Data {
@XmlElementRef(name = "DataSourceSubstitutionGroupHead", type = JAXBElement.class)
protected List<JAXBElement<?>> dataSourceSubstitutionGroupHead;
public List<JAXBElement<?>> getDataSourceSubstitutionGroupHead() {
if (dataSourceSubstitutionGroupHead == null) {
dataSourceSubstitutionGroupHead = new ArrayList<JAXBElement<?>>();
}
return this.dataSourceSubstitutionGroupHead;
}
}