The new Spring Boot 3.x upgrade (which also means an upgrade of Java to Java 17) has changed the namespaces from "javax" to "jakarta".
I am getting an exception when I run my application because it is still referencing the "javax" namespace :
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlElement
at com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector.<init>(JaxbAnnotationIntrospector.java:137)
at com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector.<init>(JaxbAnnotationIntrospector.java:124)
Looking at the stack, it references "javax" instead of "jakarta"
As far as I can tell 2.16.0 is the highest version that jackson-module.jaxb
goes to.
How can I use Jackson if it's not compatible with the Spring upgrade?
If you use the Jakarta version of JAXB, as used by Spring Boot 3, you should not use that
jackson-module-jaxb-annotations
module, you should use thejackson-module-jakarta-xmlbind-annotations
module instead.The jakarta-xmlbind in the module name refers to the Jakarta EE 9+ variant of JAXB, using the
jakarta.xml.bind
namespace instead of thejavax.xml.bind
namespace of Java EE 8 and older.See also on the FasterXML jackson-modules-base repository, which refers to the jaxb module as "Old" (
java.xml.bind
) annotations and the jakarta-xmlbind module as New "Jakarta" (jakarta.xml.bind
).The reason these modules coexist is because not everyone has upgraded to libraries or tools using Jakarta EE 9+ libraries, so Jackson wants to support both the old and the new namespace simultaneously, and that requires separate libraries/modules.
In short:
Remove
jackson-module-jaxb-annotations
from your dependenciesAdd
jackson-module-jakarta-xmlbind-annotations
to your dependenciesReplace references to
JaxbAnnotationModule
(if any) withJakartaXmlBindAnnotationModule
Replace references to
JaxbAnnotationIntrospector
(if any) withJakartaXmlBindAnnotationIntrospector
Use
gradle dependencies
ormvn dependency:tree
(or with./gradlew
or./mvnw
) to find out if anything else is pulling injackson-module-jaxb-annotations
. You'll likely need to replace or upgrade those dependencies as well.For example, as pointed out in the comments by Alex, you might have
com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider
on your classpath, which will need to be replaced withcom.fasterxml.jackson.jakarta.rs:jackson-jakarta-rs-json-provider
.