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

你。 提交于 2019-12-22 08:37:15

问题


I used JAXB to bind my xsd's and then tried creating the JAXBContext:

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

But JAXB gives 180 IllegalAnnotationsException.

Most of the exceptions have the following messages:

  1. XXX is an interface, and JAXB can't handle interfaces
  2. XXX does not have a no-arg default constructor
  3. @XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.

When I look at the classes generated none of them are interfaces and I can't understand why JAXB is interpreting them as interfaces.

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

<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 :

<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 :)


回答1:


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.

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. 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.




回答2:


Have you annotated all root classes with the @XmlRootElement annotation?

See: Unofficial JAXB Guide - Mapping interfaces - Java.net

Second, I would recommend you not to create your JAXBContext via 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:

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



回答3:


I suspect that you are trying to use a JAXB 1 (JSR-31) model with a JAXB 2 (JSR-222) runtime. In JAXB 1 implementations generated spec defined interfaces backed by implementation specific impl clases. In JAXB 2 this became spec defined classes with standard annotations that are compatible with all implementations. 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.



来源:https://stackoverflow.com/questions/11503237/why-does-jaxb-say-xxx-is-an-interface-and-jaxb-cant-handle-interfaces-even

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!