What is Eclipse Vert.x?
Vert.x is an open source, reactive software development toolkit from the developers of Eclipse. Reactive programming is a programming paradigm, associated with asynchronous streams, which responds to any changes or events.
Vert.x supports multiple JVM and non-JVM languages like:
There are two ways to start a Vert.x project: 1) go to https://start.vertx.io/ or add the following dependency to a Maven project:
The components in Vert.x are called Verticles. Verticles are pieces of code that Vert.x engine executes. Vert.x provides abstract vertical class which can be extended and implemented. Once we extend the AbstractVerticle class we must implement the start method which is called when we deploy the verticle. The start method will be executed immediately when we deploy the Verticle. Deploying Verticles can be achieved through Vertx object.
The start method has one argument which is a promise and on the end of the implementation we have to either call .complete() method if everything worked as it should, or .fail() if something went wrong. Once we run the application, the code will write “Deployed MainVerticle” on the console.
A Vert.x application is made up of one or more verticles, and each verticle forms a unit for processing asynchronous events. When we need to work with multiple verticles in our applications then we also need communication between these verticles. Verticles communicate with each other by sending messages on the event bus.
Building REST API
To start building REST API we need to add the web dependency of Vert.x.
First, we need to create the HTTP server with the following code:
You can see that at requestHandler method we are providing a router. In the router we map the REST Endpoints to methods that will return the data.
We can obtain the Router object from the static method Router.router() and as argument we need to pass the Vert.x object. Then in the router we have the methods for the HTTP Methods (GET,POST,PUT,DELETE .. etc.) and we can define a handler for that endpoint. Then in the handler we have the corresponding methods, for example the create method:
From the RoutingContext we can obtain the request body as JSON and it will map it to the Book class. We can see that this method returns a Future from the bookService. In the Book Service we have a method create that accepts Book as argument and It sends it to the repository to insert the book to the database.
To update/save/delete the database we use the SqlTemplate.forUpdate method, and to retrieve data we use the forQuery method. In this project we are using PostgreSQL as a database in docker, and flyway for managing migrations on the database.
HTTP POST -> http://localhost:8085/api/v1/books
Get All Books
HTTP GET -> http://localhost:8085/api/v1/books
In this article we delved into the Vert.x toolkit and its capabilities, including a demonstration of how to create a basic REST API with it. If you’re interested in exploring the project further, the source code is available on GitHub: https://github.com/petrovskimario/vertx-rest-api