java - 尚未找到 JAXB-API 的实现(运行 java jar 时)

标签 java spring spring-boot jaxb jax-ws

我在行中收到错误 (JAXBContext.newInstance):

    @Override
    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
    SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();

    try {
        JAXBContext context = JAXBContext.newInstance(AuthHeader.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(authentication, soapHeader.getResult());

    } catch (JAXBException e) {
        System.out.println(e.getMessage());
        throw new IOException("error while marshalling authentication.");
    }
}

当它作为测试用例执行时它运行良好:
 mvn install

或者
mvn:spring:boot 运行

但是在使用 jar 运行时会导致问题:
java -jar target/fileName-0.0.1-SNAPSHOT.jar 

运行 java -jar 并使用 postman 命中时出错。
Implementation of JAXB-API has not been found on module path or classpath.

java.io.IOException: error while marshalling authentication.

版本信息
mvn -version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-1051-aws", arch: "amd64", family: "unix"

java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

同一个 jar 在开发服务器上运行良好,但不能在登台服务器上运行。开发和登台服务器都具有相同的环境(java 版本,mvn,一切都相同)。我将这些依赖项添加到 pom.xml 中:
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.3</version>
    </dependency>

如果它与 fat jar 相关,那么为什么其他依赖项(如 amazon s3、stripe 等)适用于普通 jar。只有 jaxb 有什么问题?

我什至尝试使用带有以下配置的 mvn 包创建胖 jar:
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <mainClass>com.patracorp.patrapay.PatrapayApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
            </execution>
        </executions>
    </plugin>

但仍然得到同样的错误。

最佳答案

1) 仔细检查您没有重复的依赖项,因为 JAXB 工件已被多次重命名。

运行 'mvn dependency:tree' 不应多次输出 JAXB 相关工件。

2)尝试使用这个 Artifact :

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

关于java - 尚未找到 JAXB-API 的实现(运行 java jar 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61108214/

相关文章:

java - 如何解决Spring集成中ftp网关的连接超时问题?

Java HTML - 通过单击更改小程序的颜色

java - JVM JIT 编译器如何优化 "duplicated"Java 代码?

java - 获取 spring oauth2 的身份验证提供程序

spring-boot - 使用 H2 数据库和 Liquibase 配置 Spring Boot

java - 如何从绑定(bind)结果验证失败返回错误响应?

java - Amazon S3 在客户端位置下载文件

java - 2 带字符的 For 循环跳过了很多输出

java - 等待直到在另一个线程中获取锁

spring - 如何使用 Hibernate 配置多个模式