简体   繁体   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

I used JAXB to bind my xsd's and then tried creating the JAXBContext: 我使用JAXB绑定我的xsd,然后尝试创建JAXBContext:

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

But JAXB gives 180 IllegalAnnotationsException. 但是JAXB给出了180 IllegalAnnotationsException。

Most of the exceptions have the following messages: 大多数例外都有以下消息:

  1. XXX is an interface, and JAXB can't handle interfaces XXX是一个接口,JAXB无法处理接口
  2. XXX does not have a no-arg default constructor XXX没有无参数的默认构造函数
  3. @XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML. @ XmlAttribute / @ XmlValue需要引用映射到XML文本的Java类型。

When I look at the classes generated none of them are interfaces and I can't understand why JAXB is interpreting them as interfaces. 当我查看生成的类时,它们都不是接口,而且我不明白为什么JAXB会将它们解释为接口。

Here's is the stack trace of one of the errors reported by 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

And this is how the type is defined in xsd : 这就是在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>

Here is the pom.xml I have used to generate the binding : 这是我用来生成绑定的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>

Please be patient with me since its the first time I'm asking a question on the web :) 自从我第一次在网上问问题以来,请耐心等待我:)

Thanks to all, it was as usual a big mistake on my part, but anyways I enjoyed using stackoverflow for the first time and will make a point to snoop around here. 多亏了所有,这对我来说是一个很大的错误,但是无论如何我还是第一次喜欢使用stackoverflow,这将成为在这里窥探的重点。

The problem was with my classpath.I was referring to a xmlbeans binding project, which had the java source with the same package and classes as the ones generated by jaxb, which were included as a jar on my classpath. 问题出在我的类路径上,我指的是一个xmlbeans绑定项目,该项目的java源代码具有与jaxb生成的包和类相同的包和类,这些包和类作为jar包含在我的类路径中。 So I had 2 classes with same name and package, but to my misery JAXB was picking the Xmlbeans class and I didn't realise that for 2 days.This is one of the oldest errors in Java and I apologise for the blunder.If anyone needs any clarification please drop a comment. 所以我有两个具有相同名称和包的类,但令我痛苦的是JAXB选择了Xmlbeans类,但我两天都没有意识到这一点。这是Java中最古老的错误之一,对此我深表歉意。需要任何澄清,请发表评论。

Have you annotated all root classes with the @XmlRootElement annotation? 您是否已使用@XmlRootElement注释对所有根类进行注释?

See: Unofficial JAXB Guide - Mapping interfaces - Java.net 请参阅: 非官方JAXB指南-映射接口-Java.net

Second, I would recommend you not to create your JAXBContext via JAXBContext.newInstance("my package name"); 其次,我建议您不要通过JAXBContext.newInstance("my package name");创建您的JAXBContext JAXBContext.newInstance("my package name"); . It is better to specify the root classes explicitly. 最好明确指定根类。 Therefore if you have two root classes named ClassA and ClassB use it this way: 因此,如果您有两个名为ClassAClassB根类,请按以下方式使用它:

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

I suspect that you are trying to use a JAXB 1 ( JSR-31 ) model with a JAXB 2 ( JSR-222 ) runtime. 我怀疑您正在尝试将JAXB 1( JSR-31 )模型与JAXB 2( JSR-222 )运行时一起使用。 In JAXB 1 implementations generated spec defined interfaces backed by implementation specific impl clases. 在JAXB 1实现中,生成了由规范实现的隐式类支持的规范定义的接口。 In JAXB 2 this became spec defined classes with standard annotations that are compatible with all implementations. 在JAXB 2中,这已成为规范定义的类,带有与所有实现兼容的标准注释。 Most JAXB 2 implementations support their own JAXB 1 models, but some additional config may be necessary, if you try to use a JAXB 1 model with a difference JAXB 2 provider you may see this type of error. 大多数JAXB 2实现都支持其自己的JAXB 1模型,但是可能需要一些其他配置,如果您尝试将JAXB 1模型与其他JAXB 2提供程序一起使用,则可能会看到这种类型的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM