Vertx 的路由器问题

Router issues with Vertx

早上好, 我从 java 的 Vertx 开始,我想创建路由示例,所以我复制了这些行,但 Router.router(vertex) 中似乎有问题:

  • The type io.vertx.rxjava.core.Vertx cannot be resolved. It is indirectly referenced from required .class files

    • The method router(Vertx) from the type Router refers to the missing type Vertx

这是我的代码:

import io.vertx.core.*;
import io.vertx.rxjava.ext.web.*;
import io.vertx.core.http.HttpHeaders;

public class Test extends AbstractVerticle{

@Override
public void start() throws Exception {

    Vertx vertx=Vertx.vertx();
    Router router = Router.router(vertx);

    Route route1 = router.route("localhost:8080/Orsys/").handler(routingContext -> {

          HttpServerResponse response = routingContext.response();
          response.setChunked(true);

          response.write("Orsys\n");

          routingContext.vertx().setTimer(5000, tid -> routingContext.next());
        });

首先 io.vertx.core.AbstractVerticle class 中的 Vertx 实例指的是 io.vertx.core.Vertx,而不是 io.vertx.rxjava.core.Vertx。所以你应该导入 io.vertx.ext.web.Router 而不是 io.vertx.rxjava.ext.web.Router,否则它不会匹配。

其次,Route#route 方法不是静态方法,因此首先您需要创建一个 Router 实例:

Router router = Router.router(vertx);

然后调用路由方法:

router.route("/Test1").handler(routingContext -> {
            HttpServerResponse response = routingContext.response();
            response.setChunked(true);
            response.write("Test1 \n");

            routingContext.vertx().setTimer(1000, tid ->  routingContext.next());
        });

请注意 route 方法 接受绝对值 URL 因此您不需要传递绝对值 URL。直接传/Test1URL路径,host和port可以在createHttpServer时配置。

另外需要注意的是,如果想直接写字节到response,需要加上response.setChunked(true)否则会抛出异常:

java.lang.IllegalStateException: You must set the Content-Length header to be the total size of the message body BEFORE sending any data if you are not using HTTP chunked encoding.

或者这样更好(总是用 end):

router.route("/Test1").handler(routingContext -> {
            HttpServerResponse response = routingContext.response();
            response.putHeader("content-type", "text/html");
            response.end("<h1>Test1</h1>");
        });

现在您已经建立了一个简单的路由,下一步是创建一个 HttpServer:

vertx.createHttpServer()
                .requestHandler(router::accept)
                .listen(8080, "127.0.0.1", res -> {
                    if (res.failed())
                        res.cause().printStackTrace();
                });

嗯,端口和主机可以传递给listen方法。

所以这是修改后的正确代码:

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

public class TestVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        Router router = Router.router(vertx);

        router.route("/Test1").handler(routingContext -> {
            HttpServerResponse response = routingContext.response();
            response.setChunked(true);
            response.write("Test1 \n");

            routingContext.vertx().setTimer(1000, tid ->  routingContext.next());
        });

        vertx.createHttpServer()
                .requestHandler(router::accept)
                .listen(8080, "127.0.0.1", res -> {
                    if (res.succeeded())
                        startFuture.complete();
                    else
                        startFuture.fail(res.cause());
                });
    }
}

运行 这通过部署verticle:

public class Application {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        TestVerticle verticle = new TestVerticle();

        vertx.deployVerticle(verticle, res -> {
            if (res.failed())
                res.cause().printStackTrace();
        });
    }
}

所以这只是一个简单的例子。如果您想构建 RESTful 服务,这篇文章可能会有所帮助:Some Rest with Vert.x

这是我使用 Vert.x Web 的 RESTful 服务示例,这也可能有帮助:https://github.com/sczyh30/todo-backend-vert.x

希望这些可以帮到你:)

Eric Zhao 的回答很棒。但我想补充一点。

我的 Vert.x 申请遇到了同样的问题。它首先说 Cannot resolve symbol 'Router'。所以我导入 io.vertx.ext.web.Router。但是我的应用程序没有导入它并说 Cannot resolve symbol 'web'Cannot resolve symbol 'Router'.

我通过将以下依赖项添加到我的 pom.xml 解决了这个问题。

Maven

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-web</artifactId>
 <version>3.6.2</version>
</dependency>

Gradle

dependencies {
 compile 'io.vertx:vertx-web:3.6.2'
}