关于从替换组生成的抽象超类,我似乎无法获得所有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 Context(触发上述异常的行)的地方:
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的地方:
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;
}
}
在从XML模式生成的模型上创建JAXBContext
时,您应该在生成的模型的包名称而不是单个类上执行此操作。这将有助于确保JAXBContext
提供所有必要的课程。
JAXBContext jc = JAXBContext.newInstance("com.escholar.reports.reportXML");
我遇到了同样的问题,所以我分享了我的解决方案:
@XmlSeeAlso()
因为子类的延迟加载,JAXB似乎不可能在适当的时候使用子类。
我的基本抽象类是FieldMapping,继承的是FileFieldMapping和DatabaseFieldMapping(为简洁起见,我省略了实现)。
@XmlRootElement(name = "fieldMapping")
@XmlSeeAlso({DatabaseFieldMapping.class,
FileFieldMapping.class})
public abstract class FieldMapping {
}
@XmlRootElement(name = "fileFieldMapping")
public class FileFieldMapping extends FieldMapping {
}
@XmlRootElement(name = "databaseFieldMapping")
public class DatabaseFieldMapping extends
FieldMapping
{
}