提问者:小点点

如何使用Vert. x WebAPI与QUUKUS合同


有人将Vert. x Web Api合约与Quarkus一起使用吗?我正在尝试使用它,但我相信我遇到了配置问题。这是我尝试过的:

由于没有关于如何配置的直接指南,我将Vert. x指南(https://vertx.io/docs/3.9.6/vertx-web-api-contract/java/#_openapi_3)与QUKUS反应式路由之一(https://quarkus.io/guides/reactive-routes#using-the-vert-x-web-router)混合在一起。这个想法是将来自OpenAPI3RouterFactory的路由器安装为QUKUS创建的主路由器的子路由。

不幸的是,当我尝试访问我的endpoint时,我收到以下错误:

2021-04-22 21:14:58,696 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (vert.x-eventloop-thread-4) HTTP Request to /api/tasks failed, error id: 0e695191-ed81-4938-bc0f-3beb2d874a34-4: java.lang.IllegalStateException: Request has already been read
    at io.vertx.core.http.impl.HttpServerRequestImpl.checkEnded(HttpServerRequestImpl.java:596)
    at io.vertx.core.http.impl.HttpServerRequestImpl.handler(HttpServerRequestImpl.java:306)
    at io.quarkus.vertx.http.runtime.ResumingRequestWrapper.handler(ResumingRequestWrapper.java:18)
    at io.vertx.ext.web.impl.HttpServerRequestWrapper.handler(HttpServerRequestWrapper.java:82)
    at io.vertx.ext.web.handler.impl.BodyHandlerImpl.handle(BodyHandlerImpl.java:86)
    at io.vertx.ext.web.handler.impl.BodyHandlerImpl.handle(BodyHandlerImpl.java:42)
    at io.vertx.ext.web.impl.RouteState.handleContext(RouteState.java:1038)
    at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:137)
    at io.vertx.ext.web.impl.RoutingContextWrapper.next(RoutingContextWrapper.java:176)
    at io.vertx.ext.web.impl.RouterImpl.handleContext(RouterImpl.java:235)
    at io.vertx.ext.web.impl.RouteImpl$$Lambda$556/0x0000000065531848.handle(Unknown Source)
    at io.vertx.ext.web.impl.RouteState.handleContext(RouteState.java:1038)
    at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:137)
    at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:132)
    at io.quarkus.vertx.http.runtime.VertxHttpRecorder$4.handle(VertxHttpRecorder.java:338)
    at io.quarkus.vertx.http.runtime.VertxHttpRecorder$4.handle(VertxHttpRecorder.java:316)
    at io.vertx.ext.web.impl.RouteState.handleContext(RouteState.java:1038)
    at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:137)
    at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:132)
    at io.quarkus.vertx.http.runtime.devmode.VertxHttpHotReplacementSetup$3.handle(VertxHttpHotReplacementSetup.java:86)
    at io.quarkus.vertx.http.runtime.devmode.VertxHttpHotReplacementSetup$3.handle(VertxHttpHotReplacementSetup.java:75)
    at io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:327)
    at io.vertx.core.impl.ContextImpl$$Lambda$642/0x00000000659026f0.handle(Unknown Source)
    at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:366)
    at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38)
    at io.vertx.core.impl.EventLoopContext$$Lambda$569/0x0000000065689a48.run(Unknown Source)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:853)

有人知道可能出了什么问题吗?这是我的代码:

package systems.lil.infra.gateway.config;

import io.swagger.v3.oas.models.Operation;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.api.contract.RouterFactoryOptions;
import io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApplicationScoped
public class OpenApi3Server {

  private static final Logger logger = LoggerFactory.getLogger(OpenApi3Server.class);

  private Vertx vertx;

  @Inject
  public OpenApi3Server(Vertx vertx) {
    this.vertx = vertx;
  }

  public void init(@Observes Router mainRouter) {

    OpenAPI3RouterFactory.create(vertx, "/api/api.yaml", ar -> {
      if (ar.succeeded()) {
        logger.info("Building OpenAPI Router Factory...");

        OpenAPI3RouterFactory routerFactory = ar.result();

        RouterFactoryOptions options = new RouterFactoryOptions();

        options
            .setOperationModelKey("operationPOJO")
            .setRequireSecurityHandlers(false);
        routerFactory.setOptions(options);

        routerFactory.addHandlerByOperationId("getTasks", routingContext -> {
          routingContext.request().pause();
          Operation operation = routingContext.get("operationPOJO");
          logger.info("Handling operation {}", operation.getOperationId());

          JsonObject object = new JsonObject("{\"operation\":\"getTasks\"}");

          routingContext.response().setStatusCode(200).setStatusMessage("OK").end(object.encodePrettily());

          logger.info("Route called");
        });

        Router router = routerFactory.getRouter();
        mainRouter.mountSubRouter("/api", router);

        logger.info("SubRouter Mounted...");
      }
    });
  }
}

共1个答案

匿名用户

从日志来看,您设置了两个BodyHandler

OpenAPI留档支持:

路由器工厂自动挂载BodyHandler来管理请求体。您可以使用setBodyHandler配置BodyHandler的实例(例如更改上传目录)。

https://vertx.io/docs/3.9.6/vertx-web-api-contract/java/#_body_handler

您可以尝试将主体处理程序从OpenAPI设置为虚拟处理程序:

routerFactory.setBodyHandler(new BodyHandler() {
                    
                    @Override
                    public void handle(RoutingContext event) {
                        event.next();
                    } ...other methods here
          });