Vertx简单HTTP服务器:无响应

时间:2019-02-06 01:24:27

标签: vert.x

我的vert.x版本是3.6.2。我的操作系统是macOS(Mojave)。

我创建了简单的HTTP服务器:

    public static void main(String[] args) {

       Vertx vertx = Vertx.vertx();
       HttpServer httpServer = vertx.createHttpServer();

       Router router = Router.router(vertx);
       router.route().handler(BodyHandler.create());
       router.get("/produce").handler(routingContext -> {
          HttpServerResponse response = routingContext.response();
          response.putHeader("content-type", "text/plain");
          response.setStatusCode(200);
          response.end("<h1>Hello world</h1>");
       });

       httpServer.requestHandler(router);

       httpServer.listen(8080);
    }

我从Eclipse IDE运行了以上代码。在Eclipse控制台中,我注意到以下vert.x日志:

  

2019年2月5日晚上8:12:50 io.vertx.core.spi.resolver.ResolverProvider   INFO:无法使用默认地址解析器作为dns解析器

无论如何,我通过浏览器(即http://localhost:8080)测试了上述HTTP服务器,但未收到任何响应。我期待看到“ Hello world”。

1 个答案:

答案 0 :(得分:0)

简短答案:创建maven项目,一切正常。

详细答案: 我找到了我问题的答案。实际上,我尝试从Eclipse运行Vertx Http服务器。我只添加了几个依赖项Jar文件,以成功编译Vertx项目。这不是一个行家项目。这是一个Eclipse Java项目。我添加了vertx-core,vertx-web,netty-handler,netty-buffer,netty-resolver,netty-transport和netty-common作为Eclipse用户库。这些还不足以运行基于Vertx的Http服务器。在运行时,Vertx Http服务器需要更多的netty- *库。

当我在Eclipse IDE中导入Vertx maven项目时,它按预期工作。

在pom上,我只是添加了vertx-web,但是它添加了许多netty- *库(即:netty-codec,netty_handler-proxy,netty-codec-socks,netty-codec-http,netty-codec- http2,netty-resolver-dns等)作为传递依赖项。