springboot整合websocket后啟動(dòng)報(bào)錯(cuò)(javax.websocket.server.ServerContainer not available)
一、場(chǎng)景
Springboot使用@ServerEndpoint來(lái)建立websocket鏈接。引入依賴(lài)。
<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(); } }
二、報(bào)錯(cuò)信息
springboot項(xiàng)目添加websocket依賴(lài)后運(yùn)行測(cè)試類(lèi)報(bào)如下錯(cuò)誤:
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
三、排查思路
報(bào)的錯(cuò)誤是創(chuàng)建ServerEndpointExporterBean失敗,原因是ServerContainer不可用,那么我們就去看到ServerContainer在ServerEndpointExporter中是怎么注入的。
點(diǎn)進(jìn)去ServerEndpointExporter()
類(lèi),
在第49行代碼打上debug跑一下
選中第50行代碼,右鍵,執(zhí)行計(jì)算,或者使用快捷鍵Crtl+U
計(jì)算結(jié)果發(fā)現(xiàn)是null
四、分析原因
為什么servletContext會(huì)返回null,定位到 ServerContainer 類(lèi),發(fā)現(xiàn)他是一個(gè)接口,那必定注入的時(shí)候是有相應(yīng)的實(shí)現(xiàn)類(lèi),點(diǎn)擊查看實(shí)現(xiàn),居然有五個(gè)實(shí)現(xiàn)類(lèi),那就可以推斷是依賴(lài)沖突導(dǎo)致不知道要注入哪個(gè)實(shí)現(xiàn),最后獲取Bean的時(shí)候返回了null。
點(diǎn)進(jìn)去getAttribute
方法,查看實(shí)現(xiàn)類(lèi)
這里有五個(gè)實(shí)現(xiàn)類(lèi),三個(gè)是tomcat的,一個(gè)是undertow的,還有一個(gè)是springframework的,所以解決辦法就是排除掉其他的實(shí)現(xiàn)類(lèi),只保留springframework
這個(gè)就行。
五、解決辦法
安裝Maven Helper
插件,打開(kāi)項(xiàng)目的pom.xml
文件,點(diǎn)擊pom文件左下角的Dependency Analyzer
排除掉多余的依賴(lài)。
最終結(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>
再次啟動(dòng),一切正常!
到此這篇關(guān)于springboot整合websocket后啟動(dòng)報(bào)錯(cuò)(javax.websocket.server.ServerContainer not available)的文章就介紹到這了,更多相關(guān)springboot websocket啟動(dòng)報(bào)錯(cuò)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot服務(wù)設(shè)置禁止server.point端口的使用
- springboot中server.ssl.key-store配置路徑的問(wèn)題小結(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開(kāi)啟server:compression:enabled(Illegal character ((CTRL-CHAR, code 31)))的問(wèn)題解決
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單的銀行管理系統(tǒng)的示例代碼
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡(jiǎn)單的銀行管理系統(tǒng),可以實(shí)現(xiàn)存款,取款,查詢(xún)等功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09Java中Spring技巧之?dāng)U展點(diǎn)的應(yīng)用
這篇文章主要介紹了Java中Spring技巧之?dāng)U展點(diǎn)的應(yīng)用,下文Spring容器的啟動(dòng)流程圖展開(kāi)其內(nèi)容的相關(guān)資料,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04Java執(zhí)行cmd命令兩種實(shí)現(xiàn)方法解析
這篇文章主要介紹了Java執(zhí)行cmd命令兩種實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07詳解Spring Data JPA動(dòng)態(tài)條件查詢(xún)的寫(xiě)法
本篇文章主要介紹了Spring Data JPA動(dòng)態(tài)條件查詢(xún)的寫(xiě)法 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例
這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例,我們需要對(duì)請(qǐng)求參數(shù)進(jìn)行非空、長(zhǎng)度、正確性進(jìn)行校驗(yàn), 本文主要講解Spring Validation 和 Hibernate Validator, 同時(shí)整合i18n(國(guó)際化)實(shí)現(xiàn)參數(shù)校驗(yàn)自動(dòng),需要的朋友可以參考下2023-10-10Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07MybatisPlus調(diào)用原生SQL的三種方法實(shí)例詳解
這篇文章主要介紹了MybatisPlus調(diào)用原生SQL的三種方法,在有些情況下需要用到MybatisPlus查詢(xún)?cè)鶶QL,MybatisPlus其實(shí)帶有運(yùn)行原生SQL的方法,我這里列舉三種,需要的朋友可以參考下2022-09-09Java Applet查找素?cái)?shù)小程序代碼實(shí)例
這篇文章主要介紹了Java Applet查找素?cái)?shù)小程序代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Mybatis環(huán)境搭建及文件配置過(guò)程解析
這篇文章主要介紹了Mybatis環(huán)境搭建及文件配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08