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

如何在Dropwizard 4(搭配Jetty 11)中配置WebSocket支持?

解决Dropwizard 4 + Jetty 11 WebSocket配置问题

第一步:清理并修正依赖

你的依赖存在版本冲突(混入了Jetty 9的websocket-server)和冗余,以下是必需的最小依赖:

<!-- Dropwizard核心(确保版本为4.x) -->
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-core</artifactId>
    <version>4.0.1</version> <!-- 替换为你使用的Dropwizard 4版本 -->
</dependency>

<!-- Jakarta WebSocket API -->
<dependency>
    <groupId>jakarta.websocket</groupId>
    <artifactId>jakarta.websocket-api</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- Jetty Jakarta WebSocket服务器实现(与Jetty 11版本匹配) -->
<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jakarta-server</artifactId>
    <version>11.0.20</version>
</dependency>

需要删除的冗余/冲突依赖:

  • websocket-server(Jetty 9版本,与Jetty 11冲突)
  • websocket-jetty-server、websocket-jetty-api、websocket-jetty-common、websocket-jetty-client(非Jakarta规范实现,Dropwizard 4+Jetty11推荐用Jakarta版本)
  • websocket-servlet(旧版Servlet API的WebSocket实现,已被Jakarta版本替代)
  • websocket-core-server、websocket-core-common、websocket-jakarta-common(已包含在websocket-jakarta-server中)

第二步:WebSocket端点实现

创建一个标准的Jakarta WebSocket端点类:

import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

@ServerEndpoint("/ws/echo")
public class EchoWebSocketEndpoint {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("WebSocket连接打开: " + session.getId());
    }

    @OnMessage
    public String onMessage(String message) {
        return "收到消息: " + message;
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("WebSocket连接关闭: " + session.getId());
    }
}

第三步:在Dropwizard中配置WebSocket

在你的Dropwizard Application类的run方法中,注册WebSocket端点:

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.eclipse.jetty.websocket.jakarta.server.JettyWebSocketServletContainerInitializer;
import jakarta.websocket.DeploymentException;

public class YourApplication extends Application<YourConfiguration> {

    public static void main(String[] args) throws Exception {
        new YourApplication().run(args);
    }

    @Override
    public void initialize(Bootstrap<YourConfiguration> bootstrap) {
        // 初始化逻辑(如果需要)
    }

    @Override
    public void run(YourConfiguration configuration, Environment environment) throws DeploymentException {
        // 注册WebSocket端点
        JettyWebSocketServletContainerInitializer.configure(environment.getServletContext(), (context, container) -> {
            // 添加你的WebSocket端点
            container.addEndpoint(EchoWebSocketEndpoint.class);
        });

        // 其他Dropwizard配置(如REST资源等)
    }
}

关键说明

  • JettyWebSocketServletContainerInitializer类属于websocket-jakarta-server依赖,清理依赖后即可正常引用。
  • Dropwizard 4默认使用Jetty 11,依赖版本需保持一致,避免版本冲突。
  • 如果你使用Dropwizard Guice,只需在Guice模块中绑定WebSocket端点类即可,不影响上述配置逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:01:06