SpringBoot集成WebSocket遇到的問題及解決
SpringBoot集成WebSocket遇到的問題
最近在項(xiàng)目中需要使用WebSocket,因?yàn)轫?xiàng)目是使用的SpringBoot架構(gòu),所以集成比較簡(jiǎn)單。
上代碼:
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,用來存放每個(gè)客戶端對(duì)應(yīng)的Session對(duì)象
*/
private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<Session>();
@OnOpen
public void onOpen(@PathParam("type") String type, Session session) {
SessionSet.add(session);
log.info("WebSocket有連接加入, 請(qǐng)求的數(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ā)生錯(cuò)誤, 原因: " + 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();
}
}客戶端就不寫了,上面的代碼也沒什么好說的,相當(dāng)簡(jiǎn)單,項(xiàng)目跑起來就完事兒咯
結(jié)果就這簡(jiǎn)單玩意兒出現(xiàn)了問題
項(xiàng)目運(yù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
報(bào)錯(cuò)了?怎么會(huì)呢,這么簡(jiǎn)單的一個(gè)玩意兒,網(wǎng)上的教程不都是這么干的嗎
研究下錯(cuò)誤,標(biāo)注了@ServerEndPoint注解的類注冊(cè)失敗,百思不得其解,百度谷歌一番,什么jdk版本、tomcat版本、需不需要注入ServerEndpointExporter等等等等,我項(xiàng)目情況都不是(jdk1.8、tomcat8+、springboot內(nèi)置容器)
后來想想是否因?yàn)轫?xiàng)目是多模塊結(jié)構(gòu)的原因,可能是jar包沖突、循環(huán)依賴,什么可能都想過,然后新建了一個(gè)springboot項(xiàng)目,只集成websocket試試,結(jié)果是正常運(yùn)行的,版本一致,環(huán)境一致,代碼一致,問題看來出在項(xiàng)目本身了
項(xiàng)目結(jié)構(gòu)
- 父工程
- A 模塊(啟動(dòng)模塊,依賴了其他所有子模塊,一些公共的配置類)
- B 模塊(業(yè)務(wù)模塊)
- ...
- ...
- Q 模塊(WebSocket模塊)
難道是WebSocket作為一個(gè)單獨(dú)模塊出的飛機(jī)? 這么扯的理由我也不管了,還是把它挪到了A 模塊去,結(jié)果問題依舊....
前后折騰了很久,終于決定跟跟源碼

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

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

