SpringBoot集成WebSocket遇到的問題及解決
SpringBoot集成WebSocket遇到的問題
最近在項目中需要使用WebSocket,因為項目是使用的SpringBoot架構(gòu),所以集成比較簡單。
上代碼:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>WebSocketServer.java
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* web socket 工具類
*
* @author alvinqiu
* @data 2018/10/24
*/
@Slf4j
@Component
@ServerEndpoint(value = "/ws/{type}")
public class WebSocketServer {
/**
* concurrent包的線程安全Set,用來存放每個客戶端對應(yīng)的Session對象
*/
private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<Session>();
@OnOpen
public void onOpen(@PathParam("type") String type, Session session) {
SessionSet.add(session);
log.info("WebSocket有連接加入, 請求的數(shù)據(jù)類型為: " + type);
try {
sendMessage("Hello WebSocket " + type);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose(Session session) {
SessionSet.remove(session);
log.info("WebSocket有連接關(guān)閉");
}
@OnError
public void onError(Throwable error) {
log.info("WebSocket發(fā)生錯誤, 原因: " + error.getMessage());
}
@OnMessage
public void onMessage(String message) {
log.info("收到來自WebSocket客戶端的消息: " + message);
}
/**
* 發(fā)送消息給客戶端
*
* @param message
* @throws IOException
*/
public void sendMessage(String message) throws IOException {
for (Session session : SessionSet) {
if (session.isOpen()) {
session.getBasicRemote().sendText(message);
}
}
}
}WebSocketConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* web socket 配置類
*
* @author alvinqiu
* @data 2018/10/24
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}客戶端就不寫了,上面的代碼也沒什么好說的,相當簡單,項目跑起來就完事兒咯
結(jié)果就這簡單玩意兒出現(xiàn)了問題
項目運行
java.lang.IllegalStateException: Failed to register @ServerEndpoint class: class com.xxx.WebSocketServer$$EnhancerBySpringCGLIB$$62689f33
javax.websocket.DeploymentException: Cannot deploy POJO class [com.xxx.WebSocketServer$$EnhancerBySpringCGLIB$$62689f33] as it is not annotated with @ServerEndpoint
報錯了?怎么會呢,這么簡單的一個玩意兒,網(wǎng)上的教程不都是這么干的嗎
研究下錯誤,標注了@ServerEndPoint注解的類注冊失敗,百思不得其解,百度谷歌一番,什么jdk版本、tomcat版本、需不需要注入ServerEndpointExporter等等等等,我項目情況都不是(jdk1.8、tomcat8+、springboot內(nèi)置容器)
后來想想是否因為項目是多模塊結(jié)構(gòu)的原因,可能是jar包沖突、循環(huán)依賴,什么可能都想過,然后新建了一個springboot項目,只集成websocket試試,結(jié)果是正常運行的,版本一致,環(huán)境一致,代碼一致,問題看來出在項目本身了
項目結(jié)構(gòu)
- 父工程
- A 模塊(啟動模塊,依賴了其他所有子模塊,一些公共的配置類)
- B 模塊(業(yè)務(wù)模塊)
- ...
- ...
- Q 模塊(WebSocket模塊)
難道是WebSocket作為一個單獨模塊出的飛機? 這么扯的理由我也不管了,還是把它挪到了A 模塊去,結(jié)果問題依舊....
前后折騰了很久,終于決定跟跟源碼

恩,就是這里annotation為null,至于為何為null還是不知道,突發(fā)奇想去看看另一個springboot項目(成功案例),

恩,annotation是有值的
發(fā)現(xiàn)了pojo的區(qū)別
(報錯)
class com.xxx.websocket.WebSocketServer$$EnhancerBySpringCGLIB$$62689f33
(成功)
class com.xxx.websocket.WebSocketServer
后面那一串是代表類被cglib轉(zhuǎn)換為了代理對象,什么情況下會被轉(zhuǎn)換為代理對象?
AOP!AOP!AOP!
簡直是如夢初醒,虎軀一震
我的 A 模塊里是寫了一個全局的log日志切面
大概是這樣:
/**
* 日志切面
*
* @author alvinqiu
* @date 2018/09/28
*/
@Slf4j
@Aspect
@Component
public class AspectLog {
@Pointcut("execution(* com.xxx..*.*(..))")
public void aspectLog() {
}
@Pointcut("execution(* com.xxx.api..*.*(..))")
public void aspectApiLog() {
}
@Pointcut("execution(* com.xxx.service..*.*(..))")
public void aspectServiceLog() {
}
@Pointcut("execution(* com.xxx.mapper..*.*(..))")
public void aspectMapperLog() {
}
...
...
}問題找出來咯,WebSocketServer這個類被切咯,以至于@ServerEndPoint注解無法注入至對應(yīng)的對象,導致報錯
解決
AOP 排除此類
總結(jié)
1. 出問題多看看源碼
2. 多與大神交流,碰撞,無論多么難以描述的問題,全世界都對,你的項目不對,也要與大神溝通,一點點的終究會得到思路
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus動態(tài)表名實現(xiàn)方法
本文主要介紹了mybatis-plus動態(tài)表名實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Spring Boot 通過 Mvc 擴展方便進行貨幣單位轉(zhuǎn)換的代碼詳解
這篇文章主要介紹了Spring Boot 通過 Mvc 擴展方便進行貨幣單位轉(zhuǎn)換,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例
本篇文章主要介紹了java將XML文檔轉(zhuǎn)換成json格式數(shù)據(jù)的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
在SpringBoot中實現(xiàn)線程池并行處理任務(wù)的方法詳解
在使用Spring Boot開發(fā)應(yīng)用程序時,我們經(jīng)常需要處理一些耗時的任務(wù),例如網(wǎng)絡(luò)請求、數(shù)據(jù)庫操作或者其他需要花費一定時間的計算任務(wù),本文將介紹如何在Spring Boot中使用線程池來實現(xiàn)任務(wù)的并行處理2023-06-06
Java實現(xiàn)word轉(zhuǎn)pdf并在關(guān)鍵字位置插入圖片
這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)word轉(zhuǎn)pdf,并在word中關(guān)鍵字位置插入圖片,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11

