欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)

 更新時間:2024年01月15日 14:57:09   作者:清如許.  
這篇文章主要介紹了springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available),通過分析錯誤信息、排查代碼和配置,找出問題的根源,并給出相應(yīng)的解決方案,感興趣的可以了解一下

一、場景

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 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

三、排查思路

報的錯誤是創(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實現(xiàn)簡單的銀行管理系統(tǒng)的示例代碼

    Java實現(xiàn)簡單的銀行管理系統(tǒng)的示例代碼

    這篇文章主要介紹了如何利用Java實現(xiàn)簡單的銀行管理系統(tǒng),可以實現(xiàn)存款,取款,查詢等功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-09-09
  • Java中Spring技巧之?dāng)U展點的應(yīng)用

    Java中Spring技巧之?dāng)U展點的應(yīng)用

    這篇文章主要介紹了Java中Spring技巧之?dāng)U展點的應(yīng)用,下文Spring容器的啟動流程圖展開其內(nèi)容的相關(guān)資料,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • Spring面向切面編程AOP詳情

    Spring面向切面編程AOP詳情

    這篇文章主要介紹了Spring面向切面編程AOP詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Java執(zhí)行cmd命令兩種實現(xiàn)方法解析

    Java執(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)條件查詢的寫法

    本篇文章主要介紹了Spring Data JPA動態(tài)條件查詢的寫法 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國際化代碼實例,我們需要對請求參數(shù)進(jìn)行非空、長度、正確性進(jìn)行校驗, 本文主要講解Spring Validation 和 Hibernate Validator, 同時整合i18n(國際化)實現(xiàn)參數(shù)校驗自動,需要的朋友可以參考下
    2023-10-10
  • Java計時器StopWatch實現(xiàn)方法代碼實例

    Java計時器StopWatch實現(xiàn)方法代碼實例

    這篇文章主要介紹了Java計時器StopWatch實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • MybatisPlus調(diào)用原生SQL的三種方法實例詳解

    MybatisPlus調(diào)用原生SQL的三種方法實例詳解

    這篇文章主要介紹了MybatisPlus調(diào)用原生SQL的三種方法,在有些情況下需要用到MybatisPlus查詢原生SQL,MybatisPlus其實帶有運行原生SQL的方法,我這里列舉三種,需要的朋友可以參考下
    2022-09-09
  • Java Applet查找素數(shù)小程序代碼實例

    Java Applet查找素數(shù)小程序代碼實例

    這篇文章主要介紹了Java Applet查找素數(shù)小程序代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • Mybatis環(huán)境搭建及文件配置過程解析

    Mybatis環(huán)境搭建及文件配置過程解析

    這篇文章主要介紹了Mybatis環(huán)境搭建及文件配置過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08

最新評論