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

Spring Boot 3升级报错:找不到jakarta.xml.ws.WebServiceContext合格Bean

Spring Boot 3.x升级后WebServiceContext注入失败的解决方法

核心原因是Spring Boot 3.x基于Jakarta EE 9+,JAX-WS相关API(包括WebServiceContext)已从Jakarta EE核心中移除,且Spring对JAX-WS的支持逻辑发生了变化,以下是具体解决步骤:

1. 引入JAX-WS实现依赖

Spring Boot 3.x不再内置JAX-WS实现,需要引入第三方JAX-WS的Spring Boot Starter,以Apache CXF为例,Maven依赖配置如下:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>4.0.0</version> <!-- 版本需适配Spring Boot 3.x,可根据实际情况调整 -->
</dependency>

2. 调整WebServiceContext注入方式

替换原来的@Resource注入,直接使用Jakarta EE的@WebServiceContext注解注入,无需Setter方法:

@jakarta.jws.WebService
public class MyEndpoint {
    // 直接用@WebServiceContext注入
    @jakarta.xml.ws.WebServiceContext
    private WebServiceContext context;

    // 业务方法中使用context提取请求信息
    public String processRequest() {
        MessageContext msgContext = context.getMessageContext();
        // 提取Servlet请求对象
        HttpServletRequest request = (HttpServletRequest) msgContext.get(MessageContext.SERVLET_REQUEST);
        // 获取自定义请求头
        String customHeader = request.getHeader("X-Custom-Header");
        // 后续业务逻辑...
        return "success";
    }
}

3. 确保Endpoint被Spring正确托管注册

需要将WebService Endpoint注册为Spring Bean,或者通过配置类发布:

@Configuration
public class WebServiceConfig {
    @Bean
    public Endpoint myServiceEndpoint(MyEndpoint myEndpoint) {
        // 使用Spring Bus创建Endpoint实例
        EndpointImpl endpoint = new EndpointImpl(SpringBusFactory.getDefaultBus(), myEndpoint);
        // 设置服务发布路径
        endpoint.publish("/my-soap-service");
        return endpoint;
    }
}

4. 彻底排查依赖冲突

即使已排除旧Spring依赖,仍需检查是否残留javax.xml.ws相关依赖,使用Maven命令查看依赖树:

mvn dependency:tree | grep javax.xml.ws

若存在相关依赖,在对应依赖中排除:

<dependency>
    <!-- 第三方依赖 -->
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:20:28