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依賴后運行測試類報如下錯誤:
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中是怎么注入的。
點進(jìn)去ServerEndpointExporter()
類,
在第49行代碼打上debug跑一下
選中第50行代碼,右鍵,執(zhí)行計算,或者使用快捷鍵Crtl+U
計算結(jié)果發(fā)現(xiàn)是null
四、分析原因
為什么servletContext會返回null,定位到 ServerContainer 類,發(fā)現(xiàn)他是一個接口,那必定注入的時候是有相應(yīng)的實現(xiàn)類,點擊查看實現(xiàn),居然有五個實現(xiàn)類,那就可以推斷是依賴沖突導(dǎo)致不知道要注入哪個實現(xiàn),最后獲取Bean的時候返回了null。
點進(jìn)去getAttribute
方法,查看實現(xiàn)類
這里有五個實現(xiàn)類,三個是tomcat的,一個是undertow的,還有一個是springframework的,所以解決辦法就是排除掉其他的實現(xiàn)類,只保留springframework
這個就行。
五、解決辦法
安裝Maven Helper
插件,打開項目的pom.xml
文件,點擊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實現(xiàn)服務(wù)器發(fā)送事件(Server-Sent Events)
- SpringBoot中的server.context-path的實現(xiàn)
- SpringBoot實現(xiàn)Server-Sent Events(SSE)的使用完整指南
- SpringBoot開啟server:compression:enabled(Illegal character ((CTRL-CHAR, code 31)))的問題解決
相關(guān)文章
Java實現(xiàn)簡單的銀行管理系統(tǒng)的示例代碼
這篇文章主要介紹了如何利用Java實現(xiàn)簡單的銀行管理系統(tǒng),可以實現(xiàn)存款,取款,查詢等功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09Java中Spring技巧之?dāng)U展點的應(yīng)用
這篇文章主要介紹了Java中Spring技巧之?dāng)U展點的應(yīng)用,下文Spring容器的啟動流程圖展開其內(nèi)容的相關(guān)資料,具有一定的參考價值,需要的小伙伴可以參考一下2022-04-04Java執(zhí)行cmd命令兩種實現(xiàn)方法解析
這篇文章主要介紹了Java執(zhí)行cmd命令兩種實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07詳解Spring Data JPA動態(tài)條件查詢的寫法
本篇文章主要介紹了Spring Data JPA動態(tài)條件查詢的寫法 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Spring Validation和Hibernate Validator結(jié)合國際化代碼實例
這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國際化代碼實例,我們需要對請求參數(shù)進(jìn)行非空、長度、正確性進(jìn)行校驗, 本文主要講解Spring Validation 和 Hibernate Validator, 同時整合i18n(國際化)實現(xiàn)參數(shù)校驗自動,需要的朋友可以參考下2023-10-10Java計時器StopWatch實現(xiàn)方法代碼實例
這篇文章主要介紹了Java計時器StopWatch實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07MybatisPlus調(diào)用原生SQL的三種方法實例詳解
這篇文章主要介紹了MybatisPlus調(diào)用原生SQL的三種方法,在有些情況下需要用到MybatisPlus查詢原生SQL,MybatisPlus其實帶有運行原生SQL的方法,我這里列舉三種,需要的朋友可以參考下2022-09-09