2012-07-16 23 views
4

我使用JAXB綁定我的XSD的,然後試圖創建的JAXBContext:爲什麼JAXB會說「xxx是一個接口,而JAXB不能處理接口」。即使生成的類不是接口

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> 

請自其第一次我問一個問題,在網絡:)

+1

發佈更多信息 - 無法從你發佈的信息中分辨出來。 – duffymo 2012-07-16 11:26:58

+0

'EnvelopeType'真的是一個界面嗎?有沒有機會嘗試在JAXB 2運行時使用JAXB 1模型? – 2012-07-17 16:05:16

+0

感謝您的回覆,我附上了我使用過的pom.xml,我的運行時也是JAXB2。 – 2012-07-18 13:01:52

回答

0

你有註解的所有根類與@XmlRootElement註釋病人和我在一起?

參見:Unofficial JAXB Guide - Mapping interfaces - Java.net

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

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

您不需要使用'@ XmlRootElement'註釋所有類。如果模型是從XML模式生成的,那麼在'String'上下文路徑上創建'JAXBContext'是最好的選擇。 – 2012-07-16 13:39:57

0

我懷疑你是試圖用一個JAXB 1(JSR-31)模型與JAXB 2(JSR-222)運行。在JAXB 1實現中生成了由實現特定的impl clases支持的spec定義的接口。在JAXB 2中,這變成了spec定義的類,它具有與所有實現兼容的標準註釋。大多數JAXB 2實現都支持它們自己的JAXB 1模型,但是一些額外的配置可能是必需的,如果您嘗試使用帶有不同JAXB 2提供程序的JAXB 1模型,您可能會看到這種類型的錯誤。

2

感謝所有人,我一如既往地犯了一個很大的錯誤,但無論如何,我很喜歡第一次使用stackoverflow,並會在這裏探討一下。

問題在於我的類路徑。我指的是一個xmlbeans綁定項目,該項目的java源代碼包含與jaxb生成的相同的包和類,它們作爲jar包含在我的類路徑中。 所以我有兩個具有相同名稱和包的類,但是對於我的痛苦JAXB選擇了Xmlbeans類,並且我沒有意識到這兩天。這是Java中最古老的錯誤之一,我爲錯誤道歉。如果任何人需要任何澄清,請放下評論。

相關問題