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

SpringBoot WebSocket请求报错No mapping for GET 求助

SpringBoot WebSocket 报错 "No mapping for GET" 问题排查与解决

我的前端是基于jQuery的网页,页面加载时发起WebSocket请求,后端为SpringBoot应用,但SpringBoot日志报错 org.springframework.web.servlet.PageNotFound : No mapping for GET。以下是前后端实现代码:

前端代码

//刷新页面时创建链接
$(function(){
    //创建webSocket链接
    var ws = new WebSocket(serverWebSocketURL + "/empPoint");
    
    ws.onopen = function(evt){
        send("Hello world!!!");
    }
    
    ws.onmessage = function(evt){
        
    }
    
    ws.onerror = function(evt){
        
    }
    
    ws.onclose = function(evt){
        //关闭链路时打开
    }
});

后端SpringBoot代码

package com.njustjjy.tpk2.endpoint;

import com.njustjjy.tpk2.config.GetHttpSessionConfig;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@Log4j2
@ServerEndpoint(value = "/empPoint",configurator = GetHttpSessionConfig.class)
@Component
public class EmpEndpoint {

    //声明session对象
    private Session session;
    //获取session
    private HttpSession httpSession;

    @OnOpen
    public void onOpen(Session session, EndpointConfig endpointConfig){
        //将局部的session赋值到全局作用域
        this.session = session;
        //获取HttpSession
        this.httpSession = (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());

    }

    @OnMessage
    public void onMessage(String message, Session session){
        System.out.print(message);
    }

    @OnError
    public void onError(Throwable err){
        log.error("发生错误:" + err);
    }
}

SpringBoot配置类代码

package com.njustjjy.tpk2.config;

import com.njustjjy.tpk2.endpoint.EmpEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebScoketConfig {
    @Bean
    public EmpEndpoint businessEndpoint(){
        return new EmpEndpoint();
    }
}

尝试过一些博客方案无效,怀疑和Tomcat对WebSocket协议的支持有关,求解决。


问题分析与解决方案

  1. 请求协议错误:前端创建WebSocket时必须使用ws://或wss://协议,如果serverWebSocketURL用的是http://,浏览器会先发起HTTP GET请求握手,但后端没有对应GET接口,直接触发报错。检查serverWebSocketURL是否正确设置为WebSocket协议地址。

  2. 端点重复注册:你在WebScoketConfig中手动创建了EmpEndpoint的Bean,而@ServerEndpoint注解本身会被SpringBoot自动扫描注册,重复注册会导致握手异常。直接删除WebScoketConfig中的businessEndpoint Bean定义,SpringBoot会自动处理@ServerEndpoint注解的端点。

  3. 依赖检查:SpringBoot默认Tomcat已内置WebSocket支持,确保pom.xml中引入了正确的starter依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
  1. 前端send方法调用错误:ws.onopen里直接调用send()是错误的,应该用WebSocket实例的方法ws.send("Hello world!!!"),否则会触发未定义错误,可能导致握手后连接异常。

  2. GetHttpSessionConfig实现检查:确保该类正确继承ServerEndpointConfig.Configurator并实现modifyHandshake方法,将HttpSession存入用户属性:

public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        config.getUserProperties().put(HttpSession.class.getName(), httpSession);
    }
}

按上述步骤调整后重启应用,WebSocket连接即可正常建立。


内容的提问来源于stack exchange,提问作者linxiaoxuan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:35:36