简体   繁体   English

Java 8 到 Java JAXB API 的 11 个迁移问题

[英]Java 8 to Java 11 migration issues with JAXB API

We are in process of migrating our projects from Java 8 to Java 11. One of the APIs is dependent on a library that utilizes JAXB. As we know JAXB was removed from JAVA 11 we started to include the JAXB dependencies in the POM of the library.我们正在将我们的项目从 Java 8 迁移到 Java 11。其中一个 API 依赖于使用 JAXB 的库。我们知道 JAXB 已从 JAVA 11 中删除,我们开始将 8981704 的 POM 依赖项包含在 8981708 中.

 <!-- 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>

All works fine when we run the API project with当我们运行 API 项目时一切正常

mvn spring-boot:run mvn spring-boot:运行

However when the API is deployed in QA servers and is started using a start script with -但是,当 API 部署在 QA 服务器中并使用启动脚本启动时 -

java -jar Sample-api-3.0.0-SNAPSHOT.jar java -jar Sample-api-3.0.0-SNAPSHOT.jar

The API throws the following error when invoking the library that is dependent on JAXB - API 在调用依赖于 JAXB 的库时抛出以下错误 -

Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
  • with linked exception:有链接异常:
 [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)

UPDATE: I added the Maven Shade plugin to generate the JAR with all the dependencies but when the line of code is executed to create the JAXBContext the error still persists - JAXBContext jaxbc = JAXBContext.newInstance(new Class[]{Abc.class});更新:我添加了 Maven Shade 插件以生成具有所有依赖项的 JAR 但是当执行代码行以创建 JAXBContext 时错误仍然存在 - JAXBContext jaxbc = JAXBContext.newInstance(new Class[]{Abc.class}) ; 在此处输入图像描述

JAXB needs javax.activation module, which became jakarta.activation after rebranding to JakartaEE. JAXB 需要javax.activation模块,在更名为 JakartaEE 后成为jakarta.activation Inlcuding this dependency should help:包括这种依赖性应该有助于:

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.0.0</version>
</dependency>

Add this dependency to POM:将此依赖项添加到 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM