Routing in Vert.x
Notice that no matter which URL we specify, we always get the same result. Of course, that's not what we want to achieve. Let's start by adding the most basic endpoint, which will only tell us that the service is up and running.
For that, we'll use Router
:
val vertx = Vertx.vertx() val router = Router.router(vertx) ...
Router
lets you specify handlers for different HTTP methods and URLs.
Now, let's add a /status
endpoint that will return an HTTP status code of 200
and a message stating OK
to our user:
router.get("/status").handler { ctx -> ctx.response() .setStatusCode(200) .end("OK") } vertx.createHttpServer() .requestHandler(router) .listen(8081)
Now, instead of specifying the request handler as a block, we will pass this...