You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用XmlToDocumentConverter Bean创建失败求助

问题排查:Java 17下Spring Boot中JAXBException类找不到的问题

问题场景

在Spring Boot应用中尝试访问外部.jar中的XmlToDocumentConverter类,已在RtgsTestConfiguration中声明该Bean:

@EnableConfigurationProperties
public class RtgsTestConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(RtgsTestConfiguration.class);

    @Bean
    @Scope("cucumber-glue")
    XmlToDocumentConverter xmlToDocumentConverter() {
        return new XmlToDocumentConverter();
    }
}

在Cucumber步骤定义类CommonSteps中注入该Bean:

public class CommonSteps extends BaseSteps {

    public CommonSteps(final RtgsConfigurationProperties configurationProperties,
                       final RtgsConfigurationService<Context> contextService,
                       final ContextWorld contextWorld,
                       final AuthenticationWorld authenticationWorld,
                       final RtgsConfigurationService<Jwt> jwtService,
                       final XmlToDocumentConverter xmlToDocumentConverter)
    {
        super(configurationProperties, contextWorld, xmlToDocumentConverter);
        this.contextService = contextService;
        this.authenticationWorld = authenticationWorld;
        this.jwtService = jwtService;
    }

    @And("I validate the response, ensuring all essential fields are correctly populated.")
    public void iValidateTheResponseEnsuringAllEssentialFieldsAreCorrectlyPopulated() {
        xmlToDocumentConverter.convertXmlToDocumentPacs004(ResponseValidation.responseFileName);
    }
}

创建Bean时抛出错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlToDocumentConverter' defined in com.santander.api.framework.rtgs.configuration.RtgsTestConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.isbanuk.iso20022.common.utils.converter.XmlToDocumentConverter]: Factory method 'xmlToDocumentConverter' threw exception; nested exception is java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBException

已尝试配置

在pom.xml中添加了JAXB相关依赖:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.0</version>
</dependency>

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

当前使用Java 17,问题仍未解决。

排查与解决方案

1. 补全依赖完整性

JAXB 3.x依赖Jakarta Activation API,添加以下依赖到pom.xml:

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

同时确认所有JAXB相关依赖的范围为compile或test(适配Cucumber测试场景),避免设置为provided等受限范围。

2. 排查依赖冲突

执行mvn dependency:tree命令查看依赖树,检查是否存在旧版javax.xml.bind相关依赖(如jaxb-api、jaxb-impl),这类旧依赖会与Jakarta命名空间的依赖冲突。若发现冲突,在对应依赖中排除旧版JAXB:

<dependency>
    <!-- 外部jar依赖 -->
    <groupId>com.isbanuk.iso20022</groupId>
    <artifactId>common-utils</artifactId>
    <version>xxx</version>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 验证外部jar的JAXB适配性

检查外部XmlToDocumentConverter所在的jar是否依赖旧版JAXB(javax.xml.bind命名空间):

  • 若是,Java 17下无法直接兼容,需联系提供商获取适配Jakarta EE 9+的版本;或使用Jakarta迁移工具对外部jar进行命名空间迁移。

4. 确认Spring Boot版本兼容性

Java 17推荐搭配Spring Boot 3.x使用,该版本完全基于Jakarta EE 9+,对JAXB 3.x的支持更完善。若使用Spring Boot 2.x,需确保版本在2.7及以上(2.7.x是最后支持Java 17的Spring Boot 2系列版本)。

5. 检查Cucumber上下文加载

  • 确认Cucumber的glue路径包含RtgsTestConfiguration所在包,确保配置类被正确加载;
  • 验证@Scope("cucumber-glue")的使用是否正确,需保证Cucumber Spring依赖版本与Spring Boot版本兼容。

内容的提问来源于stack exchange,提问作者Srirangam Harshitha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 15:45:21