簡體   English   中英

JAXB為什么說“ xxx是一個接口,而JAXB無法處理接口”。 即使生成的類不是接口

[英]Why does JAXB say “xxx is an interface, and JAXB can't handle interfaces”. Even though the generated class is not an interface

我使用JAXB綁定我的xsd,然后嘗試創建JAXBContext:

JAXBContext jaxbContext = JAXBContext.newInstance("my package name");

但是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

這就是在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>

<xs:simpleType name="AcknowledgementCodeType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="m"/>
        <xs:enumeration value="p"/>
    </xs:restriction>
</xs:simpleType>

這是我用來生成綁定的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>

自從我第一次在網上問問題以來,請耐心等待我:)

多虧了所有,這對我來說是一個很大的錯誤,但是無論如何我還是第一次喜歡使用stackoverflow,這將成為在這里窺探的重點。

問題出在我的類路徑上,我指的是一個xmlbeans綁定項目,該項目的java源代碼具有與jaxb生成的包和類相同的包和類,這些包和類作為jar包含在我的類路徑中。 所以我有兩個具有相同名稱和包的類,但令我痛苦的是JAXB選擇了Xmlbeans類,但我兩天都沒有意識到這一點。這是Java中最古老的錯誤之一,對此我深表歉意。需要任何澄清,請發表評論。

您是否已使用@XmlRootElement注釋對所有根類進行注釋?

請參閱: 非官方JAXB指南-映射接口-Java.net

其次,我建議您不要通過JAXBContext.newInstance("my package name");創建您的JAXBContext JAXBContext.newInstance("my package name"); 最好明確指定根類。 因此,如果您有兩個名為ClassAClassB根類,請按以下方式使用它:

JAXBContext.newInstance(new Class[] {ClassA.class, ClassB.class});

我懷疑您正在嘗試將JAXB 1( JSR-31 )模型與JAXB 2( JSR-222 )運行時一起使用。 在JAXB 1實現中,生成了由規范實現的隱式類支持的規范定義的接口。 在JAXB 2中,這已成為規范定義的類,帶有與所有實現兼容的標准注釋。 大多數JAXB 2實現都支持其自己的JAXB 1模型,但是可能需要一些其他配置,如果您嘗試將JAXB 1模型與其他JAXB 2提供程序一起使用,則可能會看到這種類型的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM