springboot整合websocket后启动报错:javax.websocket.server.ServerContainer not available

一、场景

Springboot使用@ServerEndpoint来建立websocket链接。引入依赖。

xml 复制代码
<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-websocket</artifactId>  
</dependency>

配置Websocket

java 复制代码
@Configuration  
@EnableWebSocket  
public class WebSocketConfig {  
  
    @Bean  
    public ServerEndpointExporter serverEndpointExporter() {  
        return new ServerEndpointExporter();  
    }  
}

二、报错信息

springboot项目添加websocket依赖后运行测试类报如下错误:

java 复制代码
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-01-15 10:27:30.908 ERROR 20552 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [org/springblade/lab/external/webScoket/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$211/1936375962.getObject(Unknown Source)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:164)
	at org.springblade.core.launch.BladeApplication.run(BladeApplication.java:49)
	at org.springblade.Application.main(Application.java:33)
Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
	at org.springframework.util.Assert.state(Assert.java:76)
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:107)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
	... 17 common frames omitted

三、排查思路

报的错误是创建ServerEndpointExporterBean失败,原因是ServerContainer不可用,那么我们就去看到ServerContainer在ServerEndpointExporter中是怎么注入的。

点进去ServerEndpointExporter()类,

在第49行代码打上debug跑一下

选中第50行代码,右键,执行计算,或者使用快捷键Crtl+U

计算结果发现是null

四、分析原因

为什么servletContext会返回null,定位到 ServerContainer 类,发现他是一个接口,那必定注入的时候是有相应的实现类,点击查看实现,居然有五个实现类,那就可以推断是依赖冲突导致不知道要注入哪个实现,最后获取Bean的时候返回了null。

点进去getAttribute方法,查看实现类

这里有五个实现类,三个是tomcat的,一个是undertow的,还有一个是springframework的,所以解决办法就是排除掉其他的实现类,只保留springframework这个就行。

五、解决办法

安装Maven Helper插件,打开项目的pom.xml文件,点击pom文件左下角的Dependency Analyzer排除掉多余的依赖。

最终结果:

xml 复制代码
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--添加以下排除方法-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    <!--websocket服务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
             <!--添加以下排除方法-->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

再次启动,一切正常!

相关推荐
程序猿DD8 分钟前
OctaFuse Gateway 2.9.0:用户限流控制、自定义转发头与管理后台升级
后端·api
三千星9 分钟前
Java开发者转型AI工程化Week 5:让RAG学会思考、看见与自检
后端
一个Ai的杂货铺12 分钟前
exit 0 也会撒谎:一个"绿灯盲跑"了一个月的定时任务,尸检报告
后端
北漂EDA码路13 分钟前
现代 C++20:move、forward 与完美转发,EDA 大型程序为什么离不开它
后端
泡海椒15 分钟前
JQuick-Java快速入门教程:轻量级规则引擎环境搭建与首个脚本运行实战
后端
stark张宇1 小时前
分布式事务最全图解(6种方案):从强一致的2PC到最终对账,彻底搞懂数据一致性
分布式·后端
IT毕设实战小研1 小时前
基于大数据的WTA职业网球赛事演变与竞技格局数据可视化分析
大数据·后端·爬虫·python·信息可视化·课程设计
IT_陈寒2 小时前
Java字符串判等踩坑记:==和equals真的不能乱用
前端·人工智能·后端
小马过河R2 小时前
微信小程序自定义登录态维护:从入门到生产级落地
后端·微信小程序·小程序·架构·登录态
Dreams_l2 小时前
RabbitMQ在SpringBoot中的应用
spring boot·rabbitmq·java-rabbitmq