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

JBoss 7.4中WebSocket无法正常处理的问题排查求助

解决JBoss 7中WebSocket 404无法连接的问题

核心问题:@ServerEndpoint注解位置错误

你将@ServerEndpoint注解标注在了接口MyService上,但JSR-356 WebSocket规范明确要求该注解必须作用于具体实现类,容器不会识别接口上的WebSocket端点配置。

修改方案

将@ServerEndpoint从接口移至实现类,接口可保留方法定义:

// 接口无需添加WebSocket相关注解
public interface MyService {
    String sayHello(String name);
    void helloOnOpen(Session session);
    void helloOnClose(CloseReason reason);
}

// 实现类添加@ServerEndpoint及方法级注解
@ServerEndpoint("/websocket/helloworld")
public class MyServiceImpl implements MyService {

    @Override
    @OnMessage
    public String sayHello(String name) {
        return "Hello " + name;
    }

    @Override
    @OnOpen
    public void helloOnOpen(Session session) {
        // 处理连接打开逻辑
    }

    @Override
    @OnClose
    public void helloOnClose(CloseReason reason) {
        // 处理连接关闭逻辑
    }
}

额外排查项

  • WAR包结构验证:确保实现类位于WEB-INF/classes目录下,JBoss 7的Undertow容器会自动扫描该路径下的WebSocket端点类;若为模块化部署,需在module.xml中显式声明WebSocket API依赖。
  • 上下文根确认:你的请求路径为ws://localhost:8080/genesys-chat/websocket/helloworld,其中/genesys-chat是应用上下文根,需确认web.xml的<display-name>或jboss-web.xml的<context-root>配置正确。
  • 依赖版本匹配:你使用的jboss-websocket-api_1.0_spec 1.0.0.Final是JSR-356 1.0版本,由于依赖范围设为provided,需确保JBoss容器自带的WebSocket实现版本与该API版本兼容,避免因版本不匹配导致端点无法识别。

验证步骤

修改后重新部署应用,查看JBoss启动日志,若出现类似Registered web socket endpoint for path /websocket/helloworld的日志,说明端点已成功注册,此时再用WebSocket客户端测试连接即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 07:55:31