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

在Quarkus应用中集成openapi-generator生成的Vertx客户端及报错解决

问题描述

在基于Vert.x的Quarkus应用中使用openapi-generator生成的Vert.x客户端代码时,遇到了上下文相关的报错。

生成的客户端代码示例

ApiClient apiClient = new ApiClient(vertx, new JsonObject());

AddressGeocodeApi addressGeocodeApi = new AddressGeocodeApiImpl(apiClient);

addressGeocodeApi.addressGeocode("json", fullAddress, API_KEY, new Handler<AsyncResult<AddressGeocodeResponse>>() { 
    @Override 
    public void handle(AsyncResult<AddressGeocodeResponse> event) {

    }
});

自行编写的集成代码

@Inject Vertx vertx;

CompletableFuture<AddressGeocodeResponse> completable = new CompletableFuture<>();

try {
    ApiClient apiClient = new ApiClient(vertx, new JsonObject());
    final AddressGeocodeApi addressGeocodeApi = new AddressGeocodeApiImpl(apiClient);

    addressGeocodeApi.addressGeocode("json", fullAddress, API_KEY, res -> {
        if (res.failed()) {
            completable.completeExceptionally(res.cause());
        } else {
            completable.complete(res.result());
        }
    });

    // 阻塞等待Vert.x工作线程返回结果
    return completable.get();
} catch (Exception e) {
    // 异常处理
} finally {
    apiClient.getWebClient().close();
}

报错信息

"Access to Context.put(), Context.get() and Context.remove() are forbidden as it can leak data between unrelated processing. Use Context.putLocal(), Context.getLocal() and Context.removeLocal() instead. Note that these methods can only be used from a 'duplicated' Context, and so may not be available everywhere."


解决方法

这个报错源于Quarkus对Vert.x全局上下文的读写限制——为了避免跨处理流程的数据泄漏,框架禁止直接操作全局Context,需要调整代码适配Quarkus的上下文模型:

  1. 移除阻塞调用completable.get()
    在Quarkus的Vert.x异步环境中,阻塞线程会破坏异步模型,还容易引发上下文错乱。改为直接返回CompletionStage/CompletableFuture,让Quarkus接管异步流程:

    @Inject Vertx vertx;
    
    public CompletionStage<AddressGeocodeResponse> geocodeAddress(String fullAddress) {
        CompletableFuture<AddressGeocodeResponse> completable = new CompletableFuture<>();
        ApiClient apiClient = new ApiClient(vertx, new JsonObject());
        try {
            AddressGeocodeApi addressGeocodeApi = new AddressGeocodeApiImpl(apiClient);
    
            addressGeocodeApi.addressGeocode("json", fullAddress, API_KEY, res -> {
                if (res.failed()) {
                    completable.completeExceptionally(res.cause());
                } else {
                    completable.complete(res.result());
                }
                apiClient.getWebClient().close(); // 在异步回调中关闭客户端
            });
    
            return completable;
        } catch (Exception e) {
            apiClient.getWebClient().close();
            completable.completeExceptionally(e);
            return completable;
        }
    }
    
  2. 用VertxContextSupport复制安全上下文
    如果必须在特定上下文中执行代码,使用Quarkus提供的VertxContextSupport复制当前上下文,确保操作在隔离的本地上下文中进行:

    import io.quarkus.vertx.core.runtime.VertxContextSupport;
    
    // 复制上下文后执行逻辑
    VertxContextSupport.subscribeAndAwait(vertx, () -> {
        // 此代码块运行在复制后的安全上下文,可正常使用Local Context方法
        ApiClient apiClient = new ApiClient(vertx, new JsonObject());
        // ... 后续客户端调用逻辑
    });
    
  3. 升级openapi-generator版本
    旧版本的openapi-generator生成的Vert.x客户端可能仍依赖全局Context操作,升级到最新稳定版,生成的代码会自动适配Quarkus的上下文限制。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:40:19