Eclipse Vert.x is a lightweight framework for developing web applications, microservices and more.
I’ve created a project using IntelliJ named VertxTest based upon a Maven project to try out the routing functionality in Vert.x.
Dependencies – the pom
Our pom.xml looks like this
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>VertxTest</groupId> <artifactId>VertxTest</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <vertx.version>3.5.0</vertx.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>io.vertx.core.Launcher</Main-Class> <Main-Verticle>com.putridparrot.MainVerticle</Main-Verticle> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource> </transformer> </transformers> <artifactSet> </artifactSet> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.5.0</version> <configuration> <mainClass>io.vertx.core.Launcher</mainClass> <arguments> <argument>run</argument> <argument>com.putridparrot.MainVerticle</argument> </arguments> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>${vertx.version}</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-unit</artifactId> <version>${vertx.version}</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> <version>${vertx.version}</version> </dependency> </dependencies> </project>
The Verticle
As you can see from this my class MainVerticle (which extends AbstractVerticle) is created in the package com.putridparrot and the code looks like this
package com.putridparrot; import io.vertx.core.AbstractVerticle; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; public class MainVerticle extends AbstractVerticle { @Override public void start() { Router router = Router.router(vertx); router.route("/hello").handler(ctx -> { ctx.response() .putHeader("content-type", "text/plain") .end("Hello " + ctx.queryParam("name")); }); vertx.createHttpServer() .requestHandler(router::accept) .listen(8080); System.out.println("HTTP server started on port 8080"); } }
Note: queryParam actually returns a List
We extend AbstractVerticle overriding the start method to handle the setting up of our routes etc.
Building and Running
We execute mvn clean install and then mvn package to generate the executable jar. Finally we can run this using java -jar target/VertxTest-1.0-SNAPSHOT-fat.jar or similar in a configuration in an IDE (for example a JAR Application configuration within Intelli J just requires the path to the JAR listed above).
Testing and more
Now we should be able to run the following from your preferred web browser or Intelli J’s REST client (or similar in another IDE).
http://localhost:8080/hello?name=Mark
We can easily add further routes and/or handle GET or POST HTTP methods. For example here’s the addition of an error route using the get method
router.get("/error") .handler(this::handleGetError);
and
private void handleGetError(RoutingContext ctx) { ctx.response() .setStatusCode(500) .end(); }
Now browsing to
http://localhost:8080/error
will display a 500 error.
Static pages
We can also add static pages by declaring a route such as
router.route("/pages/*") .handler((StaticHandler.create("pages")));
where the pages directory is located off the root off of the project folder (in this example). Simply place your HTML pages in this folder and you can access them using the following URL
http://localhost:8080/pages/index.html
Routing based upon mime type
Along with routine based upon the specific query/resource requested we can also route based upon the Content-Type within a request. For example if using HTTP POST with Content-Type application/json we would want to return JSON, likewise if Content-Type is application/xml we’d expect XML return (and so on).
To route based upon the mime type we simply write the following
router.route() .consumes("*/json") .handler(ctx -> { System.out.println("JSON request"); }); router.route() .consumes("*/xml") .handler(ctx -> { System.out.println("XML request"); });
Obviously in the examples above we’re simply routing off of the root “/” URL. Supply a path within the route method, as per previous example for other routing scenarios.
Running our Vertx application
We’ve seen we can create and run the JAR, but we can also create a Vertx application by creating a main method that looks something like this
public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new MainVerticle()); }
We use deployVerticle along with our Verticle and Vertx does the rest.