How to Update An Application Using JAXB from JDK 8 to JDK 11

Photo of Rudy De Busscher by Rudy De Busscher

The Java Architecture for XML Binding (JAXB) framework is used to easily convert between XML and Java class instances.  You just have to define some annotations on the Java classes and properties and the framework uses those definitions to convert between them. But is also heavily used within the Java EE specification Java API for XML Web Services (JAX-WS) that implements the support for SOAP messages within Java Enterprise.

JAXB was integrated within the JVM itself, so you didn't need to add any code dependency to have the functionality within your application. But with the modularization performed in JDK 9, it was removed as the maintainers wanted to have a smaller JDK distribution that only contains the core concepts. That is also the reason why JavaFX was removed.

But what does that mean for your application if it uses JAXB and you want to run it on JDK 11? In this blog, we see the simple change you need to perform with the Payara Platform products to upgrade the application from JDK 8 to JDK 11.

Why XML?

Several project teams have decided to move away from using XML in their application. They mention various reasons, such as the limited support for XML by JavaScript when they want to use it in JavaScript front-ends, and also the removal of the JAXB support from the JVM is mentioned in some cases.

And it is correct that using the JSON format for exchanging data between the JavaScript front-end and the REST endpoints on the server is more suitable than XML. But XML has several benefits over JSON, especially when you have to deal with financial services or some older systems. XML supports a very wide range of data, and not only text and numbers, as it is a markup language. This gives you the possibility to send images and graphics, for example, in a standard way. JSON also only supports UTF-8 character encoding and does not allow for comments. And also worth mentioning is the support for namespaces and the many security-related aspects that are available for XML but not for JSON.

JSON is the preferred format in some cases but XML certainly has a lot of benefits in some cases and thus JAXB is still important in today's development. So what do you need to run JAXB on JDK 11?

JAXB with JDK 11

As mentioned before, you didn't need any additional dependencies for your application when you were using JDK 8 to compile your project. Annotations like javax.xml.bind.annotation.XmlRootElement and XmlRootElement are resolved from within the JDK classes themselves.

When you switch to JDK 11 for the compilation, you will see that you encounter an error that the package could not be found. Adding this as a dependency to your maven project, for example, solves this problem.

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
    <scope>provided</scope>
</dependency>

Those JAXB annotations are available in this way and your application compiles again smoothly.

JAXB and Payara

As you can see in the above snippet, we have defined the dependency with the scope provided. So it will not be packaged within the WAR or EAR file together with your application.  We have included them already for you within the Payara Products. And the reason is simple, JAX-WS is part of the Java EE and Jakarta EE full profile, so it should be simple for all our users to use any of the Enterprise specifications.

Conclusion

Now that the next LTS release of Java is around the corner,  September 2021 with JDK 17, it is time to move away from the Java 8 version if you haven't done it yet. The removal of JAXB from the core Java platform isn't a large problem if you are using it in your application, directly or as part of JAX-WS for the SOAP support.  You only need an additional dependency when you compile and package the project. At runtime, Payara already covers the removal of those classes for you.

No Nonsense Guide to JVM Implementations: OpenJDK, OpenJ9, GraalVM

Download Guide

 

 

Comments