我使用JAXB绑定我的XSD的,然后试图创建的JAXBContext:为什么JAXB会说“xxx是一个接口,而JAXB不能处理接口”。即使生成的类不是接口
JAXBContext jaxbContext = JAXBContext.newInstance("my package name");
但JAXB使180 IllegalAnnotationsException。
大部分的异常有以下消息:
- XXX是一个接口,而JAXB无法处理接口
- XXX没有一个无参数的默认构造函数
- @ 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>
请自其第一次我问一个问题,在网络:)
发布更多信息 - 无法从你发布的信息中分辨出来。 – duffymo 2012-07-16 11:26:58
'EnvelopeType'真的是一个界面吗?有没有机会尝试在JAXB 2运行时使用JAXB 1模型? – 2012-07-17 16:05:16
感谢您的回复,我附上了我使用过的pom.xml,我的运行时也是JAXB2。 – 2012-07-18 13:01:52