SpringBoot 引入 WebSocket
maven 添加websocket ,按照一篇博文引入 websocket
https://www.cnblogs.com/likun10579/p/5450209.html
在浏览器控制台测试是否连通
1 | VM 386: 1 WebSocket connection to 'ws://localhost:8080/hello' failed: Error during WebSocket handshake: Unexpected response code: 404 |
已知服务器其它接口能够连通,所以不存在url错误的原因,那为什么会报错?有可能是因为我用的springboot的原因?依赖引入应该跟原版有所不同
添加
@Component
发现依旧是 404
一开始引入的依赖是 :
1 2 3 4 5 6 7 8 9 10 11 12 | <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version> 1.1 </version> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version> 7.0 </version> </dependency> |
后修改为
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency><br><br> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version> 7.0 </version> <scope>provided</scope> </dependency> |
添加websocket配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import java.io.Serializable; /** * @xxx * @Description //TODO websocket 配置类 * @Date 10:56 **/ @Configuration public class WebSocketConfig implements Serializable { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } } |
重新启动 连接OK
分类:
后端 - 问题
标签:
websocket
, Springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具