springboot整合websocket部署报错:org.springframework.web.socket.server.standard‘

博主分享了在SpringBoot中整合WebSocket遇到的问题,配置`WebSocketConfig`和`MyWebSocket`时,发现`ServerEndpointExporter`找不到。通过排除依赖、代码调整和环境排查,最终发现问题在于不同Maven版本的影响。解决方法是更换Maven并确保环境配置正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot整合websocket部署报错:Error creating bean with name 'serverEndpointExporter',org.springframework.web.socket.server.standard找不到?

第一步
加入依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
 </dependency>

第二步
@Configuration
public class WebSocketConfig {

@Bean
public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
}

}

第三步
**/

@Component
@ServerEndpoint(value = “/ws/csrpmed/websocket/{userId}”)
public class MyWebSocket extends BaseController {

private static final Logger logger = LoggerFactory.getLogger(MyWebSocket.class);
// 以用户名为key,Session为值保存
public static final Map<String, Session> clients = new ConcurrentHashMap<>();

private Gson gson = new Gson();

/**
 * 收到客户端消息后调用的方法
 * @param message 消息内容
 * @param session
 */
@OnMessage
public void onMessage(String message, Session session) {
}
/**
 * 连接发生错误时的调用方法
 * @param session
 * @param error
 */
@OnError
public void onError(Session session, Throwable error) {
    try {
        //发送对象
        String from = session.getPathParameters().get(Util.USERNAME);
        logger.error("---------------------与{}的连接发生错误---------------------",from);
    } catch (Exception e) {
        //logger.error("聊天异常!", e);
        //重试连接会一直刷屏
    }
}
/**
 * 建立连接
 */
@OnOpen
public void onOpen(@PathParam(Util.USERID) String userId, Session session) {
    try {
        logger.info("用户聊天连接入参 {}",userId);
        //将用户添加到容器
        clients.put(userId, session);
        addOnlineCount();     //在线数加1
        logger.info("用户聊天连接成功用户id为 {}",userId);
    } catch (Exception e) {
        //logger.error("聊天连接异常", e);
        //重试连接会一直刷屏
    }
}
/**
 * 连接关闭
 */
@OnClose
public void onClose(Session session) {
    try {
        logger.info("用户关闭连接入参 {}", session);
        String username = session.getPathParameters().get(Util.USERNAME);
        // 移除容器中存储的用户数据以及发消息通知其他用户
        if (clients.get(username) != null) {
            clients.remove(username);
            subOnlineCount();     //在线数减1
        }
        logger.info("用户 {} 退出聊天剩余连接 :{}", username, clients);
    } catch (Exception e) {
        logger.error("用户退出聊天异常", e);
    }
}

// 推送消息给某个人
public static  void sendMessageTo(String message, String to) {
    Session session = clients.get(to);
    if (session != null) {
       session.getAsyncRemote().sendText(message);
    }
}

// 推送消息给某个人,非异步
public static Session sendMessageToSynchronized(String message, String to) {
    Session session = clients.get(to);
    if (session != null) {
        try {
            session.getBasicRemote().sendText(message,true);
            logger.info("同步发送消息成功");
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("同步发送消息失败"+e);
        }
    }
    return session;
}

public static synchronized void addOnlineCount() {
    MyWebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
    MyWebSocket.onlineCount--;
}
}

按着这样来依然会出现这个问题
第一步中WebSocketConfig这个类找不到?
org.springframework.web.socket.server.standard

开始在网上搜了…
1注释第一步中的类.
2去掉第三步的@Component

但是运行了,依然报错…

开始自己以为是,jar包没下载全,然后清空了maven本地仓库,重新下载,还是如此…
开始以为是自己代码写的问题,请求小组大佬在他电脑上可以运行…

在这里插入图片描述

我猜测是环境问题…没错就是这样.
首先重启idea…不行
重启电脑…不行…
重新拉代码…不行
最后抱着侥幸的态度换了个maven(idea自带的)… 结果奇迹般的运行了…
然后我又换回自己曾经那个maven… 结果可以…

在这里插入图片描述
好吧,总算是圆满解决了,亲测有效果,有知道原理大佬,可以在评论区留言,谢谢各位!!!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值