You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot WebSocket应用连接报错404,请求技术支持

Fixing WebSocket 404 Handshake Error Between Cross-Origin Client and Spring Boot Server

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 WebSocketHandler class is annotated with @Component so Spring can detect and inject it into your config.
  • Make sure WebSocketConfig has both @Configuration and @EnableWebSocket annotations—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:22:01