WebSocket connection to '*' failed: Error during WebSocket handshake: Unexpected response code: 404

本文详细介绍了在SpringBoot2.0中配置WebSocket时可能遇到的错误:连接失败及404错误,并提供了排查步骤与解决方案,包括检查路径、配置ServerEndpointExporter以及将WebSocket的bean托管给spring容器。

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

Spring boot 2.0配置WebSocket 出现WebSocket connection to 'ws://localhost:8080/websocket/3c9d8e1e0cf24bfdbc65b8f24367def3' failed: Error during WebSocket handshake: Unexpected response code: 404

 

1.首先排查路径是否正确;

2.如果路径没有问题, 检查是否配置ServerEndpointExporter;

@Component
public class WebSocketConfig {
    /**
     * ServerEndpointExporter 作用
     *
     * 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
     *
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

WebSocket 注册的bean默认是自己管理,没有托管给spring。需要将WebSocket的bean托管给spring容器。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值