Sub router cannot be mounted on an exact path

扶醉桌前 提交于 2021-01-29 18:55:53

问题


I would like to do nested routes in vert.x and I have tried the following:

class MainVerticle : AbstractVerticle() {

  private fun apiResource(r: Router): Unit {
    r.route("/api")
      .subRouter(GenderResource(vertx).create())
  }

  private fun rootResource(r: Router): Unit {
    r.route("/").handler {
      val res = it.response()
      res.putHeader("content-type", "text/plain");

      // Write to the response and end it
      res.end("I am healthy");
    }
  }

  override fun start(startPromise: Promise<Void>) {
    val server = vertx.createHttpServer()

    val router = Router.router(vertx)

    apiResource(router)
    rootResource(router)
    server.requestHandler(router).listen(8888) { http ->
      if (http.succeeded()) {
        startPromise.complete()
        println("HTTP server started on port 8888")
      } else {
        startPromise.fail(http.cause());
      }
    }
  }
}

when I start to compile the app, I've got error messsage:

java.lang.IllegalStateException: Sub router cannot be mounted on an exact path.
        at io.vertx.ext.web.impl.RouteImpl.subRouter(RouteImpl.java:153)
        at io.databaker.MainVerticle.apiResource(MainVerticle.kt:13)
        at io.databaker.MainVerticle.start(MainVerticle.kt:31)
        at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$5(DeploymentManager.java:196)
        at io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:96)
        at io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:59)
        at io.vertx.core.impl.EventLoopContext.lambda$runOnContext$0(EventLoopContext.java:40)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Thread.java:834)

The code that causes the error is:

r.route("/api")
 .subRouter(GenderResource(vertx).create())

The GenderResource is defined as follows:

class GenderResource(private val vertx: Vertx) {

  private val router = Router.router(vertx)

  init {
      readAll()
  }

  private fun readAll(): Unit {

    val router = Router.router(vertx)

    router
      .get("/genders")
      .handler {
        val res = it.response()
        res.putHeader("content-type", "text/plain");

        // Write to the response and end it
        res.end("I am genders path");
      }
  }

  fun create(): Router {
    return router
  }
}

I am trying to create a /api/genders path and in the future I will also create /api/interests. So making /api/* as root path for public interfaces.

Regarding to error above, What am I doing wrong?


回答1:


You already suggested a solution for that.

The way to go:

r.route("/api/*")
      .subRouter(GenderResource(vertx).create())
      .subRouter(InterestsResource(vertx).create())


来源:https://stackoverflow.com/questions/64733202/sub-router-cannot-be-mounted-on-an-exact-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!