我知道,这个问题似乎是一个重复的问题:JAXB解编组期间的InstantiationException (带有@XmlSeeAlso具体子类的抽象基类)
但是,情况略有不同:我们使用maven-jaxb2-plugin从xsd-文件生成我们的Java。你可以找到他们这里。
在我们的pom中,我们使用以下配置(在2.3.1版本中使用jaxb-api ):
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>schema2-generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<arg>-Xnamespace-prefix</arg>
</args>
<useActiveProxyAsHttpproxy>true</useActiveProxyAsHttpproxy>
<proxyHost>your.host</proxyHost>
<proxyPort>your.port</proxyPort>
<strict>false</strict>
<encoding>UTF-8</encoding>
<catalog>src/main/resources/xsd/xbau/v22/catalog.cat</catalog>
<schemaDirectory>src/main/resources/xsd/xbau/v22</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/resources/xsd/xbau/v22/xjb</bindingDirectory>
<bindingIncludes>
<bindingInclude>externalBindings.xjb</bindingInclude>
</bindingIncludes>
<generatePackage>our.generated.package.xbau.v22</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/xbau/v22</generateDirectory>
</configuration>
</execution>
</executions>
</plugin>JAXB正在生成以下类:
Nachrichtenkopf.G2G2 (子类):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Nachrichtenkopf.G2G")
public class NachrichtenkopfG2G2
extends NachrichtenkopfG2G
{
}Nachrichtenkopf.G2G (超类):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Nachrichtenkopf.G2G", namespace = "http://www.osci.de/xinneres/basisnachricht/4", propOrder = {
"identifikationNachricht",
"leser",
"autor"
})
@XmlSeeAlso({
NachrichtenkopfG2G2 .class
})
public abstract class NachrichtenkopfG2G {
@XmlElement(name = "identifikation.nachricht", namespace = "", required = true)
protected IdentifikationNachricht2 identifikationNachricht;
@XmlElement(namespace = "", required = true)
protected Behoerde leser;
@XmlElement(namespace = "", required = true)
protected BehoerdeErreichbar autor;
...
}现在,如果我们为"Nachrichtenkopf“接收一个没有xsi类型的XML (请参见下面),我们将得到一个InstantiationException,因为解组程序由于缺少类型信息而无法创建一个具体实例:
<?xml version="1.0" encoding="utf-8"?>
<xbau:beteiligung.aufforderung.0300 xmlns="http://www.xleitstelle.de/xbau/2/2/" produkt="Produkt" produkthersteller="Hersteller" produktversion="Version1" standard="XBau" version="2.2" xmlns:xbau="http://www.xleitstelle.de/xbau/2/2">
<nachrichtenkopf xmlns=""> <!-- Missing Type information: xsi:type="xbau:Nachrichtenkopf.G2G" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
...
</nachrichtenkopf>
...基本上,我有两个问题:
为了完整起见,externalBindings.xjb的内容
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:annox="http://annox.dev.java.net"
xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix"
jaxb:version="2.1">
<!-- Bemerkung: Die Schemagenerierung hat hier dazu geführt, dass es in der Factory
zwei gleichnamige Methoden gibt, daher werden die Methoden hier explizit benannt -->
<jaxb:globalBindings fixedAttributeAsConstantProperty="true"/>
<jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd">
<jaxb:bindings node="xs:group[@name='arcModel']">
<jaxb:bindings node=".//xs:element[@ref='xlink:title']">
<jaxb:property name="arcModelTitle"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings node="xs:group[@name='locatorModel']">
<jaxb:bindings node=".//xs:element[@ref='xlink:title']">
<jaxb:property name="locatorModelTitle"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings node="xs:element[@name='arc']">
<jaxb:factoryMethod name="arc42"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../xbau-baukasten.xsd">
<jaxb:bindings node="xs:complexType[@name='Nachrichtenkopf.G2G']">
<jaxb:class name="NachrichtenkopfG2G2"/>
</jaxb:bindings>
<jaxb:bindings>
<namespace:prefix name="xbau" />
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://www.osci.de/xinneres/basisnachricht/4/xinneres-basisnachricht.xsd">
<jaxb:bindings node="xs:complexType[@name='Nachricht.G2G']">
<jaxb:class name="NachrichtG2G2"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://www.osci.de/xinneres/basisnachricht/4/xinneres-basisnachricht.xsd">
<jaxb:bindings node="xs:complexType[@name='Identifikation.Nachricht']">
<jaxb:class name="IdentifikationNachricht2"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://www.osci.de/xinneres/kommunikation/3/xinneres-kommunikation.xsd">
<jaxb:bindings node="xs:complexType[@name='Code.Erreichbarkeit']">
<jaxb:class name="CodeErreichbarkeit2"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://www.osci.de/xinneres/kommunikation/3/xinneres-kommunikation.xsd">
<jaxb:bindings node="xs:complexType[@name='Kommunikation']">
<jaxb:class name="Kommunikation2"/>
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd">
<jaxb:bindings node="xs:complexType[@name='arcType']">
<jaxb:class name="ArcType2"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>发布于 2022-04-29 17:04:05
用目录文件解决了这个问题。that file(查看我最初问题中的第一个代码块)包含一个条目,建议模式解析器查找导致本地文件系统中问题的方案,而不是在线查找。如果您有兴趣在xsd-解析(jaxb -如何将xsd文件映射到URL以找到它们)上下文中将在线资源重写为本地资源,您可以查看这篇文章。
通过这种方式,我可以更改现在驻留在我的src/main/resources文件夹中的方案。所讨论的方案是http://www.osci.de/xinneres/basisnachricht/4/xinneres-basisnachricht.xsd。我将name="Nachrichtenkopf.G2G" abstract="true"更改为name="Nachrichtenkopf.G2G" abstract="false",这样类Nachrichtenkopf.G2G不再是抽象的,因此可以实例化。
https://stackoverflow.com/questions/69811016
复制相似问题