Web sub-routers

We are going to add new route handlers to the HttpServerVerticle. While we could just add handlers to the existing router, we can also take advantage of sub-routers. They allow a router to be mounted as a sub-router of another one, which can be useful for organizing and/or re-using handlers.

Here is the code for the API router:

  1. Router apiRouter = Router.router(vertx);
  2. apiRouter.get("/pages").handler(this::apiRoot);
  3. apiRouter.get("/pages/:id").handler(this::apiGetPage);
  4. apiRouter.post().handler(BodyHandler.create());
  5. apiRouter.post("/pages").handler(this::apiCreatePage);
  6. apiRouter.put().handler(BodyHandler.create());
  7. apiRouter.put("/pages/:id").handler(this::apiUpdatePage);
  8. apiRouter.delete("/pages/:id").handler(this::apiDeletePage);
  9. router.mountSubRouter("/api", apiRouter); (1)
  1. This is where we mount our router, so requests starting with the /api path will be directed to apiRouter.