背景
最近在项目上用了WebSocket,但是遇到了一个很奇怪的问题,应用启动时抛出
Failed to register @ServerEndpoint class
配置类
package com.smec.fin.config.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @Description:
* @author: wei.wang
* @since: 2020/7/28 10:29
* @history: 1.2020/7/28 created by wei.wang
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
接收连接的类
package com.smec.fin.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @Description:
* @author: wei.wang
* @since: 2020/7/28 10:46
* @history: 1.2020/7/28 created by wei.wang
*/
@ServerEndpoint("/websocket")
@Component
public class WebSocketServer {
private static Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
/**
* 静态变量,用来记录当前在线连接数。
*/
private static volatile int onlineCount = 0;
/**
* concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象,目前只有一个客户端,所以使用Set
*/
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
/**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session;
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
//加入set中
webSocketSet.add(this);
//在线数加1
addOnlineCount();
logger.info("开始监听,当前在线人数为{}", getOnlineCount());
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
logger.info("有一连接关闭!当前在线人数为" + getOnlineCount());
//从set中删除
webSocketSet.remove(this);
//在线数减1
subOnlineCount();
logger.info("有一连接关闭!当前在线人数为" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message) {
logger.info("收到信息:" + message);
System.out.println("webSocketSet Size " + webSocketSet.size());
}
/**
* 发送消息
*
* @param message
*/
public void sendMessageToPad(String message) {
for (WebSocketServer item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
logger.error("发生错误");
error.printStackTrace();
}
/**
* 实现服务器主动推送
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
看起来都没问题,我们把异常信息都打出来
Caused by: javax.websocket.DeploymentException: Cannot deploy POJO class [com.smec.fin.service.impl.WebSocketServer$$EnhancerBySpringCGLIB$$6b71ea8] as it is not annotated with @ServerEndpoint
at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:245)
at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:228)
at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:156)
... 17 common frames omitted
问题原因
问题定位到WsServerContainer.java:245行,打上断点,我们发现pojo被代理了,其实根据打印日志com.smec.fin.service.impl.WebSocketServer$$EnhancerBySpringCGLIB$$6b71ea8也可以看出来,使用了CGLIB代理,那么问题就很明显了,此类被AOP做了代理,在AOP中将此类移除就可以了。
正常的pojo
被代理的pojo
AOP配置
将WebSocketServer类从AOP中移除,项目即可正常启动
@AfterThrowing(pointcut = "execution( * com.smec.fin.service..*.*(..)) && !execution(* com.smec.fin.service.impl.WebSocketServer.*(..))", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Throwable e) {
logger.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL");
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。