SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

2024-04-18 1454阅读

原标题:SpringBoot在使用WebSocket时遇到Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available的解决办法

这是异常堆栈:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [xxxxx]: 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:1796) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at com.wuqihuyu.sockettest.websockettest.WebSocketTestApplication.main(WebSocketTestApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
	at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:107) ~[spring-websocket-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 16 common frames omitted
进程已结束,退出代码1

可见大部分堆栈都是在源码里面报的错,此时我们就需要进源码里面看看了,直接进最后一个堆栈的所在位置

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)

我们下断点调试可以发现这个方法是一个公共方法,很多类的初始化都会在这里调用

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

于是我们就需要使用条件断点,断点名称,即beanname必须包含:serverEndpointExporter这个才断点

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

一步步往里面走

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

发现它会调用afterPropertiesSet方法

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

在这里我们可以看到假设getServerContainer为null则会抛出异常,我们看下这个getServerContainer到底返回什么东西

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

它会把本类的serverContainer直接返回,此时我就有疑问了,这个serverContainer为什么为空呢,我们就需要查找它到底在哪设置值了

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

这个serverContainer是一个私有变量,即可知道它肯定只能在本类里面通过set赋值,我们直接关键字搜索:

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

有两个地方,但是我们不知道到底执行哪一个,我们直接两个都下断点,然后重新启动

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

断点停在了initServletContext方法,在这我们可以看到因为ServletContext为空,所以就给它设置了空值,但ServletContext为什么会为空呢,我们点进去看一下

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

发现它是一个接口,既然ServletContext是一个接口它肯定有很多实现类,我们看一下都有哪些

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

除了springboot和tomcat的,还有一个io.undertow.servlet.spec的实现类,这就找到了问题了,估计就是依赖冲突造成springboot在加载的时候加载了错误的ServletContext实现类导致的,搜索了一下果然有一个

        
            io.undertow
            undertow-websockets-jsr
            1.4.17.Final
        

直接删掉这个依赖重新运行,发现一切正常。

SpringBoot与WebSocket遇到 javax.websocket.server.ServerContainer not available不可用

版权所有:XuanRan

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]