Spring Boot WebSocket应用连接报错404,请求技术支持
Let's break down the 404 error you're seeing—it almost always boils down to a mismatch in endpoint paths or missing cross-origin configuration. Here's how to troubleshoot and fix it step by step:
1. Double-Check Your WebSocket Endpoint Mapping
First, verify that the endpoint path in your WebSocketConfig matches what your client is trying to connect to.
If your WebSocketConfig looks like this (without a custom context path):
@Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private WebSocketHandler webSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/marco"); } }
Then your client's connection URL should be ws://localhost:8080/marco—not ws://localhost:8080/SpringWebSocket/marco. The /SpringWebSocket part is only needed if you've explicitly set a server context path in application.properties or application.yml:
# application.properties server.servlet.context-path=/SpringWebSocket
If you did set this, confirm the context path is active by checking your server startup logs for a line like:
Tomcat started on port(s): 8080 (http) with context path '/SpringWebSocket'
2. Add Cross-Origin (CORS) Configuration
Since your client runs on port 8090 and the server on 8080, you need to explicitly allow cross-origin WebSocket connections. Update your WebSocketConfig to include allowed origins:
@Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/marco") // Allow your client's origin (use "*" only for development) .setAllowedOrigins("http://localhost:8090"); }
Without this, the server will reject the handshake request, which can sometimes manifest as a 404 error in the browser (even though the actual issue is a CORS restriction).
3. Validate Your WebSocket Handler and Config Setup
- Ensure your
WebSocketHandlerclass is annotated with@Componentso Spring can detect and inject it into your config. - Make sure
WebSocketConfighas both@Configurationand@EnableWebSocketannotations—these are required for Spring to enable WebSocket support.
4. Test the Endpoint with a Tool
To rule out client-side issues, test the WebSocket endpoint directly using a tool like wscat (install via npm install -g wscat):
# If no context path wscat -c ws://localhost:8080/marco # If using context path /SpringWebSocket wscat -c ws://localhost:8080/SpringWebSocket/marco
If this command also returns a 404, the problem is definitely on the server side—double-check your config and endpoint mapping again.
5. Confirm Pom.xml Dependencies
Make sure you have the Spring WebSocket starter in your pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
This ensures all necessary WebSocket libraries are included and the embedded Tomcat is configured to support WebSocket connections.
Start with these steps—most likely the issue is either a mismatched endpoint path or missing CORS configuration. Once you fix those, the handshake should succeed.
内容的提问来源于stack exchange,提问作者iftekhar khan

