document.write("");

SpringBoot 引入 WebSocket

maven 添加websocket ,按照一篇博文引入 websocket

 https://www.cnblogs.com/likun10579/p/5450209.html

 

在浏览器控制台测试是否连通

1
VM386: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

 

posted @   人间春风意  阅读(209)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示

距今时间:
1025天8.00 小时 52.89 分钟

当前新增阅读数:140327