RxJava无法为路由调用处理程序

时间:2018-06-18 20:38:42

标签: java rx-java reactive-programming

我正在使用RxJava和Vert.x来调用“/ callme”的路由,如下所示。当我打电话给“/ callme”时,它仍会打印“你好,从rx verticle!”

@Override
public void start() throws Exception {

    HttpServer server = vertx.createHttpServer();

   ` server.requestStream().toObservable().subscribe(req -> {
        HttpServerResponse res = req.response();
        res.putHeader("content-type", "text/html");
        res.end("Hi there, from rx verticle!");
    });

    server.listen(8090);

    Router router = Router.router(Vertx.vertx());
    router.route().handler(BodyHandler.create());

    // Bind "/" to our hello message - so we are still compatible.
    router.route("/").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response
            .putHeader("content-type", "text/html")
            .end("<h1>Hello from my first Vert.x 3 application</h1>");
    });

    router.route("/callme*").handler(BodyHandler.create());
    router.post("/callme").handler(this::divide);
}

1 个答案:

答案 0 :(得分:1)

您总是获得相同响应的原因是您创建了路由器,但您从未将其注册到服务器。

@Override
public void start() throws Exception {

    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(Vertx.vertx());
    router.route().handler(BodyHandler.create());

    // Bind "/" to our hello message - so we are still compatible.
    router.route("/").handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response
            .putHeader("content-type", "text/html")
            .end("<h1>Hello from my first Vert.x 3 application</h1>");
    });

    router.route("/callme*").handler(BodyHandler.create());
    router.post("/callme").handler(this::divide);

        server.requestStream().toObservable().subscribe(req -> {
         HttpServerResponse res = req.response();
         res.putHeader("content-type", "text/html");
         res.end("Hi there, from rx verticle!");
     });

         vertx
        .createHttpServer()
        .requestHandler(router::accept)
        .rxListen(8090)
        .subscribe(httpServer -> logger.info("server is running..."));
}
相关问题