api - 如何在没有 tomcat 或任何类型的服务器的情况下在 intellij 上启动 api 应用程序?
问题描述
我有一个使用 vert.x 框架创建的 API 应用程序,我能够构建应用程序但无法运行。当我尝试运行该应用程序时,我会自动重定向到“cucumber.api.cli.main not found 错误”。我删除了自动配置,但下次我尝试运行它会生成的应用程序。我应该运行什么配置。
我曾尝试对此进行研究,但大多数问题和答案都要求我设置我不想做的汤姆猫服务器或玻璃鱼服务器。
解决方案
这是我使用 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。
推荐阅读
- php - DomPDF 删除页脚中的多余空间
- scala - Scala 中的模式转换
- vba - Powerpoint 插件:如何创建可下载的插件来修改功能区和调用宏?
- android - 从 Uri 创建 DocumentFile
- c# - 向我的 Xamarin 应用程序聊天添加实时功能会引发 System.InvalidOperationException
- google-cloud-storage - 我们可以在将对象复制到新存储桶时保留存储类吗?
- android - 有没有办法在查询“MediaStore.Audio.Media.EXTERNAL_CONTENT_URI”之前强制重新扫描系统媒体?
- dataset - CSE-CIC-IDS2018
- python - 将 Panda Column 转换为由换行符分隔的文本文件
- ios - 为什么出口默认声明为弱?