为什么JAXB说“xxx是一个接口,而JAXB无法处理接口”。即使生成的类不是接口

Alo*_*dna 5 java binding jaxb

我使用 JAXB 绑定我的 xsd,然后尝试创建 JAXBContext:

JAXBContext jaxbContext = JAXBContext.newInstance("my package name");
Run Code Online (Sandbox Code Playgroud)

但是 JAXB 给出了 180 IllegalAnnotationsException。

大多数异常都有以下消息:

  1. XXX是一个接口,JAXB无法处理接口
  2. XXX 没有无参数默认构造函数
  3. @XmlAttribute/@XmlValue 需要引用映射到 XML 中文本的 Java 类型。

当我查看生成的类时,它们都不是接口,并且我无法理解为什么 JAXB 将它们解释为接口。

以下是 JAXB 报告的错误之一的堆栈跟踪:

com.sc.md.datatypes.schemas.csemessage.EnvelopeType is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
    at com.sc.md.datatypes.schemas.csemessage.EnvelopeType
    at protected com.sc.md.datatypes.schemas.csemessage.EnvelopeType com.sc.md.datatypes.schemas.csemessage.cseMessage.envelope
    at com.sc.md.datatypes.schemas.csemessage.cseMessage
com.sc.md.datatypes.schemas.csemessage.EnvelopeType does not have a no-arg default constructor.
this problem is related to the following location:
    at com.sc.md.datatypes.schemas.csemessage.EnvelopeType
    at protected com.sc.md.datatypes.schemas.csemessage.EnvelopeType com.sc.md.datatypes.schemas.csemessage.cseMessage.envelope
    at com.sc.md.datatypes.schemas.csemessage.cseMessage
Run Code Online (Sandbox Code Playgroud)

这就是 xsd 中类型的定义方式:

<xs:complexType name="EnvelopeType">
    <xs:sequence>
        <xs:element name="Sent" type="DateTimeType"/>
        <xs:element name="Identifier" type="String_1_14"/>
        <xs:element name="AcknowledgementCode" type="AcknowledgementCodeType"/>
    </xs:sequence>
Run Code Online (Sandbox Code Playgroud)

<xs:simpleType name="AcknowledgementCodeType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="m"/>
        <xs:enumeration value="p"/>
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)

这是我用来生成绑定的 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cs</groupId>
<artifactId>cs-jaxb</artifactId>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.4-1</version>
    </dependency>
</dependencies>
<name>cs jaxb</name>
<version>1.0.0</version>
<parent>
    <artifactId>hip-jaxb-parent</artifactId>
    <groupId>com.cs</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>

            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.0</version>
            <executions>
                <execution>

                    <id>CS</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>src/main/resources/</schemaDirectory>
                        <schemaIncludes>
                            <include>**/*.xsd</include>
                        </schemaIncludes>

                    </configuration>
                </execution>

            </executions>
        </plugin>
        <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

请耐心等待,因为这是我第一次在网上提问:)

Rob*_*ert 0

您是否已使用注释对所有根类进行了注释@XmlRootElement

请参阅:非官方 JAXB 指南 - 映射接口 - Java.net

其次,我建议您不要通过创建 JAXBContext JAXBContext.newInstance("my package name");。最好明确指定根类。因此,如果您有两个命名的根类ClassAClassB以这种方式使用它:

JAXBContext.newInstance(new Class[] {ClassA.class, ClassB.class});
Run Code Online (Sandbox Code Playgroud)