springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)
一、場景
Springboot使用@ServerEndpoint來建立websocket鏈接。引入依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
配置Websocket
@Configuration
@EnableWebSocket
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
二、報錯信息
springboot項目添加websocket依賴后運(yùn)行測試類報如下錯誤:
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 failedorg.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
三、排查思路
報的錯誤是創(chuàng)建ServerEndpointExporterBean失敗,原因是ServerContainer不可用,那么我們就去看到ServerContainer在ServerEndpointExporter中是怎么注入的。
點(diǎn)進(jìn)去ServerEndpointExporter()類,
在第49行代碼打上debug跑一下

選中第50行代碼,右鍵,執(zhí)行計算,或者使用快捷鍵Crtl+U

計算結(jié)果發(fā)現(xiàn)是null

四、分析原因
為什么servletContext會返回null,定位到 ServerContainer 類,發(fā)現(xiàn)他是一個接口,那必定注入的時候是有相應(yīng)的實(shí)現(xiàn)類,點(diǎn)擊查看實(shí)現(xiàn),居然有五個實(shí)現(xiàn)類,那就可以推斷是依賴沖突導(dǎo)致不知道要注入哪個實(shí)現(xiàn),最后獲取Bean的時候返回了null。
點(diǎn)進(jìn)去getAttribute方法,查看實(shí)現(xiàn)類

這里有五個實(shí)現(xiàn)類,三個是tomcat的,一個是undertow的,還有一個是springframework的,所以解決辦法就是排除掉其他的實(shí)現(xiàn)類,只保留springframework這個就行。
五、解決辦法
安裝Maven Helper插件,打開項目的pom.xml文件,點(diǎn)擊pom文件左下角的Dependency Analyzer排除掉多余的依賴。

最終結(jié)果:
<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服務(wù)-->
<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>
再次啟動,一切正常!
到此這篇關(guān)于springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)的文章就介紹到這了,更多相關(guān)springboot websocket啟動報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot服務(wù)設(shè)置禁止server.point端口的使用
- springboot中server.ssl.key-store配置路徑的問題小結(jié)
- 使用Java和SpringBoot實(shí)現(xiàn)服務(wù)器發(fā)送事件(Server-Sent Events)
- SpringBoot中的server.context-path的實(shí)現(xiàn)
- SpringBoot實(shí)現(xiàn)Server-Sent Events(SSE)的使用完整指南
- SpringBoot開啟server:compression:enabled(Illegal character ((CTRL-CHAR, code 31)))的問題解決
相關(guān)文章
Spring boot框架下的RabbitMQ消息中間件詳解
這篇文章詳細(xì)介紹了Spring Boot框架下的RabbitMQ消息中間件的基本概念、消息傳輸模型、環(huán)境準(zhǔn)備、Spring Boot集成以及消息生產(chǎn)和消費(fèi),感興趣的朋友跟隨小編一起看看吧2025-01-01
Mybatis實(shí)現(xiàn)分頁的注意點(diǎn)
Mybatis提供了強(qiáng)大的分頁攔截實(shí)現(xiàn),可以完美的實(shí)現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進(jìn)行分頁所遇到的問題及注意點(diǎn),需要的朋友一起看看吧2017-07-07
IntelliJ IDEA設(shè)置Tabs實(shí)現(xiàn)同時打開多個文件且分行顯示
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA設(shè)置Tabs實(shí)現(xiàn)同時打開多個文件且分行顯示,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
IDEA解決maven包沖突easypoi NoClassDefFoundError的問題
這篇文章主要介紹了IDEA解決maven包沖突easypoi NoClassDefFoundError的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
springboot實(shí)現(xiàn)postman中form-data傳參實(shí)現(xiàn)過程
在項目場景開發(fā)中,對接需求要求使用POST請求傳form-data數(shù)據(jù),使用HttpClient請求時遇到了415錯誤,后改為restTemplate,封裝參數(shù)時使用LinkedMultiValueMap對象以解決參數(shù)無法正確解析的問題2026-04-04
SpringBoot Arthas實(shí)現(xiàn)線上監(jiān)控診斷
本文主要介紹了SpringBoot Arthas實(shí)現(xiàn)線上監(jiān)控診斷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-03-03

