Vert.x Web And Request Routing

In yesterday's post, we started looking at handlers; but we only showed how to set up a handler for ALL HTTP requests. It would be nice if there were an easy way to attach a handler to SPECIFIC HTTP requests. There is, and it's called Vert.x Web. Vert.x Web has a number of additional features above and beyond the Core features:

  • Session Handling
  • Authentication Hooks
  • Static Content Handling
  • Localization
  • Cookies Handling
  • Websocket Extensions
  • Error Handlers
  • Timeout Handlers
  • HTML Templating Hooks
  • Cross-Origin Request Handling
  • Virtual Hosts (Both HTTP AND HTTPS)
  • OAuth Hooks

But most importantly, Vert.x Web has a component called a Router. That will be the focus of today's post.

Using Routers

So far, we have seen that we can add a requestHandler() to an HTTP server, but what if we want to have a number of different paths which do different things in our web application? This is where the Vert.x Web module comes in. It gives us a new features like Router and RoutingContext.

Exercise.groovy
import io.vertx.lang.groovy.GroovyVerticle
import io.vertx.groovy.ext.web.Router
import io.vertx.groovy.ext.web.RoutingContext
import io.vertx.core.json.JsonObject

class Exercise extends GroovyVerticle {

    void start() {
        def router = Router.router(vertx)

        router.get('/')              .handler(this.&rootHandler)
        router.get('/something/else').handler(this.&otherHandler)

        vertx.createHttpServer()             // Create a new HttpServer
             .requestHandler(router.&accept) // Register a request handler
             .listen(8080, '127.0.0.1')      // Listen on 127.0.0.1:8080
    }

    void rootHandler(RoutingContext ctx) {
        ctx.response().end(new JsonObject([ok: true, path: ctx.request().path()]).encode())
    }

    void otherHandler(RoutingContext ctx) {
        ctx.response().end(new JsonObject([ok: false, message: 'Something Else']).encode())
    }
}
  1. You see that we added 2 different routes to the Router instance
  2. Each route has a separate handler set via a method reference
  3. Finally, we pass the Router’s accept method via a method reference as a handler for the HttpServer’s requestHandler() method.

Routes with Path Parameters

In the previous example, we saw that we could specify different paths with different handlers, but what about if we want to capture information FROM the path in a programmatic manner?

Exercise.groovy
import io.vertx.lang.groovy.GroovyVerticle
import io.vertx.groovy.ext.web.Router
import io.vertx.groovy.ext.web.RoutingContext
import io.vertx.core.json.JsonObject

class Exercise5 extends GroovyVerticle {

    void start() {
        def router = Router.router(vertx)

        router.get('/')            .handler(this.&rootHandler)
        router.get('/customer/:id').handler(this.&custHandler)

        vertx.createHttpServer()            // Create a new HttpServer
            .requestHandler(router.&accept) // Register a request handler
            .listen(8080, '127.0.0.1')      // Listen on 127.0.0.1:8080
    }

    void rootHandler(RoutingContext ctx) {
        ctx.response().end(new JsonObject([ok: true, path: ctx.request().path()]).encode())
    }

    void custHandler(RoutingContext ctx) {
        ctx.response().end(new JsonObject([
           ok: false,
           custID: ctx.request().getParam('id')
        ]).encode())
    }
}
  1. Modify the example above to have a new route which had multiple path parameters
  2. Modify the example above to use a route with regular expressions
  3. Modify the example to add a new HTTP POST endpoint which consumes JSON and produces the POSTed JSON
That should give you a pretty good start on using the Router in Vert.x Web. Tune in tomorrow for even more Vert.x goodness!

Comments

This is a complete tech blog post. I do not understand anythinmg related to tech but by brother and husband is a tech junkie, will be surely sharing with them. Thank you.

Popular Posts