Vert.x: A lightweight Java toolkit.

V KALYAN RAM
4 min readApr 10, 2024

--

Java developers can easily use Vert.x to build reactive and event-driven applications on the JVM.

Vert.x allows the reactive programming paradigm, allowing developers to build applications that react to events and handle asynchronous operations in a non-blocking manner.

Vert.x can certainly be used for handling HTTP requests and building HTTP-based applications. It provides a lightweight and efficient HTTP server component that allows developers to create web applications, RESTful APIs, microservices, and other HTTP-based services.

Vert.x includes a built-in HTTP server that allows you to handle HTTP requests and serve HTTP responses without the need for an external servlet container or web server. This makes it easy to create standalone HTTP applications.

(When we say “standalone” in the context of Vert.x’s built-in HTTP server, we mean that you can create and deploy HTTP applications without the need for an external web server or servlet container)

Note: If you’re using Vert.x to build your Java web applications, you don’t necessarily need to use Apache Tomcat or any other external servlet container. Vert.x includes its own built-in HTTP server component, which eliminates the need for a separate servlet container.

Add these above-mentioned dependencies to your build.gradle file (Here I am using version 4.2.4. If you are going to use a different version, see to that you make changes accordingly in the program down).

A Java program to handle incoming HTTP requests and send a response in the form of plain text “Hello from vert.x!”.

Program 1:

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;

public class Main {
public static void main(String[] args) {
// Create a Vert.x instance
Vertx vertx = Vertx.vertx();
// Create an HTTP server
HttpServer server = vertx.createHttpServer();

// Handle incoming HTTP requests
server.requestHandler(request -> {
// Write a response
request.response()
.putHeader("content-type",
"text/plain")
.end("Hello from Vert.x!");
});

// Listen on port 8080
server.listen(8080,result ->{
if (result.succeeded()) {
System.out.println("Server is now listening on port 8080");
} else {
System.err.println("Failed to start server: "+result.cause());
}
});
}
}

A Java program to handle incoming HTTP requests and send a response in the form of JSON object.

Program 2:

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;

public class Main{

public static void main(String[] args) {
// Create a Vert.x instance
Vertx vertx = Vertx.vertx();

// Create an HTTP server
HttpServer server = vertx.createHttpServer();

// Define request handler
server.requestHandler(request -> {
// Set content type header to JSON
request.response().putHeader("Content-Type", "application/json");

// Create a JSON object
JsonObject json = new JsonObject()
.put("message", "Hello, Vert.x!")
.put("timestamp", System.currentTimeMillis());

// Write JSON object to response
request.response().end(json.encodePrettily());
});

// Start the server
server.listen(8080, result -> {
if (result.succeeded()) {
System.out.println("HTTP server started on port 8080");
} else {
System.err.println("Failed to start HTTP server");
}
});
}
}

A Java program to make a simple HTTP GET request to the server with the endpoint “http://localhost:8080”(since in the above programs we have used port 8080. You can use the port number of your wish and make changes accordingly).

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

public static void main(String[] args) {
try {
// Create a URL object with the endpoint you want to call
URL url = new URL("http://localhost:8080");

// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set request method to GET
connection.setRequestMethod("GET");

// Get the response code
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);

// Read the response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();

// Print the response
System.out.println("Response Body: " + response.toString());

// Close the connection
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

So you first create two Java projects. You use the first project for running the Vert.x server code and second project for sending HTTP requests to the server. Run the Vert.x code, where the server starts to run and then run the second project and get the response output.

OUTPUT 1:

OUTPUT 2:

If you found this article valuable, I encourage you to share it with others who may also benefit from its insights. If you have any questions, thoughts, or experiences you’d like to share, please don’t hesitate to reach out — I’d love to hear from you!

Do follow me !!

Thank you and have a nice day!!!

--

--