javax to jakarta migration: glassfish JAXB 4.x reads jakarta.xml.bind.annotations* but not javax.xml.bind.annotations.*

485 views Asked by At

The context

I'm unmarshalling XML to Java using the following library:

<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
  <version>2.3.1</version>
</dependency>

The unmarshalling code looks like:

 MyClass instance = JAXBContext.newInstance(MyClass.class)
                               .createUnmarshaller()
                               .unmarshal(new StreamSource(getClass().getClassLoader()
                                    .getResourceAsStream("myfile.xml")), MyClass.class)
                               .getValue();

It's working well.

The issue

I'm trying to migrate to the version 4.0.2 of the library:

 <dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
  <version>4.0.2</version>
</dependency>

When running the code, an error occurs:

Two classes have the same XML type name "someType". Use @XmlType.name and @XmlType.namespace to assign different names to them.

The analysis

In my class hierarchy has two classes "SomeType" (in different namespace). The two classes are annotated by javax.xml.bind.annotation.XmlType. The class hierarchy is packaged as a library and is shared amongst several applications.

When running with jaxb-runtime 2.3.1, the JAXB class ClassInfoImpl reads the javax.xml.bind.annotation.XmlType annotation:

    this.elementName = this.parseElementName(clazz);
    XmlType t = (XmlType)this.reader().getClassAnnotation(XmlType.class, clazz, this);
    this.typeName = this.parseTypeName(clazz, t);

But when running with jaxb-runtime 4.0.2, the JAXB class ClassInfoImpl reads the new jakarta.xml.bind.annotation.XmlType (notice the jakarta prefix instead of the javax prefix).

Of course the XmlType annotation cannot be found (because it's not the same package), so the error occurs.

The question

Is there another solution than update the classes from my library by changing from javax to jakarta annotations ?

1

There are 1 answers

2
Laurent Schoelens On BEST ANSWER

JAXB 4 is part of JakartaEE 10 whereas JAXB 2.3 is part of JavaEE 8

The major change is the change of packages from javax.* to jakarta.* starting from JakartaEE 9 (see here for details about this huge change in Java ecosystem).

The eclipse foundation that is now in charge from JakartaEE has created this tool (eclipse transformer) for helping users with that kind of migration where it's difficult to change codebase where users wants to go with JakartaEE world.

If you cannot change your java source (based on javax packages), and this source is bundled in a specific jar that is used by your application (which should be the case since jaxb4 is jdk11 baseline and you didn't complain about compilation error about undefined javax.xml.bind.annotation.XmlType since jaxb-runtime is no more included in JDK starting from JDK9), you may be interested to use the eclipse maven transformer plugin.

Hope that will help you get it work as wanted.