DevTUPLE

Basic Maven project for Vertx

Vertx is an open source and reactive framework. It is a polyglot framework which means it supports JVM and non-JVM based languages.

Steps for creating the basic vertx project:

  1. Create a maven project.
  2. The maven dependency for vertx framework can be given as.
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-web</artifactId>
  <version>3.5.1</version>
</dependency>
  1. Create an instance for the class Vertx by calling Vertx.vertx() and HttpServer by calling vertx.createHttpServer() as given below.
Vertx vertx=Vertx.vertx();
HttpServer httpServer=vertx.createHttpServer();
  1. Create a Router instance by passing the vertx instance name to the Router.router() method as given below.
Router router=Router.router(vertx);
  1. Now we can create routes using this router variable.

The sample route can be given as

Using get method
Route handler1=router
               .get("/hello/:name")
               .handler(routingContext ->{
            	   String name=routingContext.request().getParam("name");
            	   HttpServerResponse response =routingContext.response();
            	   response.setChunked(true);
            	   response.write("Hi "+name+ " from get\n");
            	   response.end();
               });

Here inside the get method the "/:name" represnets that the name is a variable name. we can access the value of the name variable from the url by

routingContext.request().getParam("name");

The HttpServerResponse insatnce is used to write the response for the get method. The output when the url is /hello/Aravind will be shell Hi Aravind from get

Using post method
Route handler2=router
                .post("/hello")
                .handler(routingContext ->{
             	   HttpServerResponse response =routingContext.response();
             	   response.setChunked(true);
             	   response.write("Hello Aravind from post\n");
             	   response.end();
                });
  1. The port number has to be configured using httpServer.listen(port_number) method.
httpServer
          .requestHandler(router::accept)
          .listen(8091);

The whole Application class of this basic project can be give as

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        Vertx vertx=Vertx.vertx();
        HttpServer httpServer=vertx.createHttpServer();
        Router router=Router.router(vertx);
        
        Route handler1=router
               .get("/hello/:name")
               .handler(routingContext ->{
            	   String name=routingContext.request().getParam("name");
            	   HttpServerResponse response =routingContext.response();
            	   response.setChunked(true);
            	   response.write("Hi "+name+ " from get\n");
            	   response.end();
               });
        Route handler2=router
                .post("/hello")
                .handler(routingContext ->{
             	   HttpServerResponse response =routingContext.response();
             	   response.setChunked(true);
             	   response.write("Hello Aravind from post\n");
             	   response.end();
                });
        
         httpServer
               .requestHandler(router::accept)
               .listen(8091);
    }
}

Written by Aravind P C

Do you want to know when I post something new?
Then subscribe to my newsletter.