Why does JAXB say: "xxx is an interface, and JAXB cannot process interfaces." Although the generated class is not an interface

I used JAXB to bind my xsd and then tried to create a JAXBContext:

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

But JAXB provides 180 IllegalAnnotationsException exceptions.

Most exceptions have the following messages:

  • XXX is an interface and JAXB cannot handle interfaces
  • XXX has no default-no-arg constructor
  • @ XmlAttribute / @ XmlValue must be referenced in a Java type that displays text in XML.

When I look at the created classes, none of them are interfaces, and I cannot understand why JAXB interprets them as interfaces.

Here 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 so 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 that I used to create 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, as the first time I ask a question on the Internet :)

+4
source share
3 answers

Thanks to everyone, as usual, it was a big mistake on my part, but in any case, I liked using stackoverflow for the first time, and I will try to find snoop here.

The problem was related to my classpath. I meant the xmlbeans binding project, which had a java source with the same package and classes as those created by jaxb that were included as jar in my classpath. Thus, I had 2 classes with the same name and package, but unfortunately JAXB chose the Xmlbeans class, and I did not understand this for 2 days. This is one of the oldest bugs in Java, and I apologize for the error. If anyone needs any clarification, please leave a comment.

+2
source

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

See: The Unofficial JAXB Guide - Mapping Interfaces - Java.net

Secondly, I would recommend that you not create a JAXBContext through JAXBContext.newInstance("my package name"); . It is better to explicitly specify the root classes. Therefore, if you have two root classes named ClassA and ClassB , use it as follows:

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

I suspect you are trying to use the JAXB 1 ( JSR-31 ) model with JAXB 2 ( JSR-222 ). JAXB 1 implementations generate specific interfaces supported by specific implementations. In JAXB 2, these became specified classes with standard annotations that are compatible with all implementations. Most JAXB 2 implementations support their own JAXB 1 models, but some additional configuration may be required if you try to use the JAXB 1 model with a different JAXB 2 provider, you may see this type of error.

0
source

All Articles