SpringBoot 引入 WebSocket
maven 添加websocket ,按照一篇博文引入 websocket
https://www.cnblogs.com/likun10579/p/5450209.html
在浏览器控制台测试是否连通
VM386:1 WebSocket connection to 'ws://localhost:8080/hello' failed: Error during WebSocket handshake: Unexpected response code: 404
已知服务器其它接口能够连通,所以不存在url错误的原因,那为什么会报错?有可能是因为我用的springboot的原因?依赖引入应该跟原版有所不同
添加
@Component
发现依旧是 404
一开始引入的依赖是 :
<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>
后修改为
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>
添加websocket配置类
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
作者:人间春风意
扫描左侧的二维码可以赞赏

本作品采用署名-非商业性使用-禁止演绎 4.0 国际 进行许可。

浙公网安备 33010602011771号