項目中用到websocket,就將原來寫好的websocket工具類直接拿來使用,發現前端建立連接的時候報404,經查找發現是因為原來用的是配置的外部tomcat啟動,這次是spring boot自帶的啟動,查找看網上說要加入以下配置:
1,添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
2,添加配置
@Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3,添加注解@Component
@ServerEndpoint(value = "/socket/{userId}") @Component public class SocketServer{}
結果啟動報錯:
Error creating bean with name 'serverEndpointedExporter' defined in class path resource
Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
查了半天發現按照網上的方法改了都沒有用,后同事提示說看看依賴是否有沖突,經過檢查發現確是因為依賴沖突了,解決了沖突之后成功啟動,可以和前端建立連接