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

Jetty WebSocket客户端迁移至Jakarta版本(Spring3适配)方案咨询

Jetty WebSocket客户端迁移至Jakarta版本方案

一、依赖变更

将原依赖org.eclipse.jetty.websocket:websocket-client替换为Jakarta版本的客户端依赖:

<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-jakarta-client</artifactId>
    <version>你的Jetty版本(建议11+)</version>
</dependency>

二、核心代码迁移

以下是对应原代码的迁移实现,关键变更点已标注:

// Let's create and start the Web Socket
//
// For internal test, we have a self-signed certificate. So we need to short cut certificate check.
// DO NOT DO THAT IN PRODUCTION!
boolean trustAll = (System.getProperty("com.graphql-java-generator.websocket.nosslcheck") != null);
org.eclipse.jetty.util.ssl.SslContextFactory.Client sslContextFactory = new org.eclipse.jetty.util.ssl.SslContextFactory.Client(trustAll);
org.eclipse.jetty.client.HttpClient httpClient = new HttpClient(sslContextFactory);

// 替换原WebSocketClient为Jakarta版本的客户端
org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClient wsClient = new org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClient(httpClient);

// 注意:SubscriptionClientWebSocket需要适配Jakarta WebSocket API
// 原类需改为实现jakarta.websocket.Endpoint,或使用@ClientEndpoint注解
SubscriptionClientWebSocket<R, T> subscriptionClientWebSocket = new SubscriptionClientWebSocket<R, T>(request,
                subscriptionName, subscriptionCallback, subscriptionType, messageType,
                graphQLRequest.getGraphQLObjectMapper());

URI uri = getWebSocketURI();
try {
    wsClient.start();

    // 替换原ClientUpgradeRequest为Jakarta的ClientEndpointConfig
    // 可通过该配置设置请求头、子协议等升级参数
    jakarta.websocket.ClientEndpointConfig clientEndpointConfig = jakarta.websocket.ClientEndpointConfig.Builder.create()
            // 示例:添加自定义请求头
            .configurator(new jakarta.websocket.ClientEndpointConfig.Configurator() {
                @Override
                public void beforeRequest(java.util.Map<String, java.util.List<String>> headers) {
                    // 此处可添加原ClientUpgradeRequest中的请求头、参数等
                    // 比如 headers.put("X-Custom-Header", List.of("value"));
                }
            })
            // 若需指定子协议,可添加:.preferredSubprotocols(List.of("graphql-ws"))
            .build();

    // 使用Jakarta客户端的connect方法建立连接
    wsClient.connect(subscriptionClientWebSocket, uri, clientEndpointConfig);
    logger.debug("Connecting to {}", uri);
} catch (Exception e) {
    String msg = "Error while opening the Web Socket connection to " + uri;
    logger.error(msg);
    throw new GraphQLRequestExecutionException(msg, e);
}

三、SubscriptionClientWebSocket适配说明

原SubscriptionClientWebSocket需适配Jakarta WebSocket API,两种方式任选:

方式1:继承Endpoint类

将原继承Jetty的WebSocketAdapter改为继承jakarta.websocket.Endpoint,并重写对应方法:

public class SubscriptionClientWebSocket<R, T> extends jakarta.websocket.Endpoint {
    // 构造方法保留原逻辑

    @Override
    public void onOpen(jakarta.websocket.Session session, jakarta.websocket.EndpointConfig config) {
        // 原onOpen逻辑迁移至此
        // 可通过session.addMessageHandler添加消息处理器
        session.addMessageHandler(new jakarta.websocket.MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                // 原onMessage逻辑迁移至此
            }
        });
    }

    // 按需重写onClose、onError方法
}

方式2:使用@ClientEndpoint注解

直接在类上标注@ClientEndpoint,并实现对应的消息处理方法:

@jakarta.websocket.ClientEndpoint
public class SubscriptionClientWebSocket<R, T> {
    // 构造方法保留原逻辑

    @jakarta.websocket.OnOpen
    public void onOpen(jakarta.websocket.Session session) {
        // 原onOpen逻辑迁移至此
    }

    @jakarta.websocket.OnMessage
    public void onMessage(String message, jakarta.websocket.Session session) {
        // 原onMessage逻辑迁移至此
    }

    @jakarta.websocket.OnClose
    public void onClose(jakarta.websocket.CloseReason reason) {
        // 原onClose逻辑迁移至此
    }

    @jakarta.websocket.OnError
    public void onError(Throwable error) {
        // 原onError逻辑迁移至此
    }
}

四、关键API差异说明

  • 原ClientUpgradeRequest的功能被拆分到ClientEndpointConfig(配置子协议、扩展)和ClientEndpointConfig.Configurator(修改请求头、自定义升级逻辑)中
  • 消息处理方式从Jetty专属的WebSocketListener/WebSocketAdapter改为Jakarta标准的Endpoint或注解式处理
  • 客户端核心类从WebSocketClient替换为JakartaWebSocketClient,但底层HttpClient的使用逻辑保持不变

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 22:25:12