我们正在将项目从 Java 8 迁移到 Java 11。其中一个 API 依赖于利用 JAXB 的库。正如我们所知,JAXB 已从 JAVA 11 中删除,我们开始将 JAXB 依赖项包含在库的 POM 中。
<!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
<!-- API -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
当我们运行 API 项目时一切正常
mvn spring-boot:运行
但是,当 API 部署在 QA 服务器中并使用带 - 的启动脚本启动时
java -jar 示例-api-3.0.0-SNAPSHOT.jar
调用依赖于 JAXB 的库时,API 抛出以下错误 -
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory] at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:269) at javax.xml.bind.ContextFinder.find(ContextFinder.java:412) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662) at com.eoriginal.ecore.client.documents.SearchDocumentHistoryAPI$RequestParameters.toMap(SearchDocumentHistoryAPI.java:344) ... 14 more Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122) at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:267)
更新: 我添加了 Maven Shade 插件来生成具有所有依赖项的 JAR,但是当执行代码行来创建 JAXBContext 时,错误仍然存在 - JAXBContext jaxbc = JAXBContext.newInstance(new Class[]{Abc.class});
JAXB 需要
javax.activation
模块,在更名为 JakartaEE 后成为 jakarta.activation
。包含这种依赖性应该会有所帮助:
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.0.0</version>
</dependency>
将此依赖项添加到 POM 中:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>