首页 > 解决方案 > 如何在没有 tomcat 或任何类型的服务器的情况下在 intellij 上启动 api 应用程序?

问题描述

我有一个使用 vert.x 框架创建的 API 应用程序,我能够构建应用程序但无法运行。当我尝试运行该应用程序时,我会自动重定向到“cucumber.api.cli.main not found 错误”。我删除了自动配置,但下次我尝试运行它会生成的应用程序。我应该运行什么配置。

我曾尝试对此进行研究,但大多数问题和答案都要求我设置我不想做的汤姆猫服务器或玻璃鱼服务器。

标签: apiintellij-ideaweb-deploymentvert.x

解决方案


这是我使用 IntelliJ Idea vert.x 的 hello world 应用程序 -

垂直:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

public class MyVerticle extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Router router = Router.router(vertx);
    router.route("/").handler(routingContext -> {
      HttpServerResponse response = routingContext.response();
      response.putHeader("content-type", "text/html")
        .end("<h1> Hello Vert.x </h>");
    });
    vertx.createHttpServer().requestHandler(router::accept)
      .listen(8070, http -> {
        if (http.succeeded()) {
          LOGGER.info("Started Server at port 8070");
        } else {
          startFuture.fail(http.cause());
        }
      });
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8888, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
    router.route("/test").handler(routingContext -> {
      HttpServerResponse response = routingContext.response();
      response.putHeader("content-type","text/html")
        .end("<h2> This is another end point with same port </h2>");
    });
    vertx.createHttpServer().requestHandler(router::accept).listen(8070,http ->{
      if(http.succeeded()){
        LOGGER.info("Another server started 8070");
      }else{
        startFuture.fail(http.cause());
      }
    });
  }

  @Override
  public void stop() {
    LOGGER.info("Shutting down application");
  }

}

部署 Verticle 的主要方法

import com.testproject.starter.verticles.MyVerticle;
import io.vertx.core.Vertx;

public class MyVerticleTest {
  public static void main(String[] args) {
    Vertx vertex = Vertx.vertx();
    MyVerticle myVerticle = new MyVerticle();
    vertex.deployVerticle(myVerticle);
  }
}

现在您可以访问以下 URL -
1. http://localhost:8888
2. http://localhost:8070/test

该应用程序不需要运行 tomcat。

参考:https ://vertx.io/docs/

有用的链接 - https://github.com/vert-x3/vertx-awesome


推荐阅读