Vert.x Core is a high-performance, reactive application toolkit for the JVM providing fundamental building blocks for asynchronous I/O operations.
—
Full-featured HTTP 1.1/2.0 client and server implementation with WebSocket support, request/response handling, streaming capabilities, and comprehensive configuration options.
Create and configure HTTP servers for handling incoming requests.
/**
* Create an HTTP server with default options
* @return HttpServer instance
*/
HttpServer createHttpServer();
/**
* Create an HTTP server with custom options
* @param options Server configuration options
* @return HttpServer instance
*/
HttpServer createHttpServer(HttpServerOptions options);
/**
* HTTP Server interface for handling requests
*/
interface HttpServer extends Measured, Closeable {
/**
* Set the request handler for all HTTP requests
* @param handler Handler to process incoming requests
* @return this for chaining
*/
HttpServer requestHandler(Handler<HttpServerRequest> handler);
/**
* Set the WebSocket handler for WebSocket upgrade requests
* @param handler Handler to process WebSocket connections
* @return this for chaining
*/
HttpServer webSocketHandler(Handler<ServerWebSocket> handler);
/**
* Start listening on a port
* @param port Port to listen on
* @return Future that completes when server is listening
*/
Future<HttpServer> listen(int port);
/**
* Start listening on a specific host and port
* @param port Port to listen on
* @param host Host to bind to
* @return Future that completes when server is listening
*/
Future<HttpServer> listen(int port, String host);
/**
* Start listening on a SocketAddress
* @param address Address to listen on
* @return Future that completes when server is listening
*/
Future<HttpServer> listen(SocketAddress address);
/**
* Get the actual port the server is listening on
* @return The port number
*/
int actualPort();
/**
* Close the server
* @return Future that completes when server is closed
*/
Future<Void> close();
/**
* Set connection handler
* @param handler Handler for new connections
* @return this for chaining
*/
HttpServer connectionHandler(Handler<HttpConnection> handler);
/**
* Set exception handler
* @param handler Exception handler
* @return this for chaining
*/
HttpServer exceptionHandler(Handler<Throwable> handler);
/**
* Set invalid request handler
* @param handler Handler for invalid requests
* @return this for chaining
*/
HttpServer invalidRequestHandler(Handler<HttpServerRequest> handler);
}Create and configure HTTP clients for making outbound requests.
/**
* Create an HTTP client with default options
* @return HttpClient instance
*/
HttpClient createHttpClient();
/**
* Create an HTTP client with custom options
* @param options Client configuration options
* @return HttpClient instance
*/
HttpClient createHttpClient(HttpClientOptions options);
/**
* Create an HTTP client builder for advanced configuration
* @return HttpClientBuilder for fluent configuration
*/
HttpClientBuilder httpClientBuilder();
/**
* HTTP Client interface for making requests
*/
interface HttpClient extends Measured, Closeable {
/**
* Create a request
* @param method HTTP method
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientRequest
*/
Future<HttpClientRequest> request(HttpMethod method, int port, String host, String requestURI);
/**
* Create a request with options
* @param options Request options
* @return Future that completes with HttpClientRequest
*/
Future<HttpClientRequest> request(RequestOptions options);
/**
* Make a GET request
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientResponse
*/
Future<HttpClientResponse> get(int port, String host, String requestURI);
/**
* Make a POST request
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientResponse
*/
Future<HttpClientResponse> post(int port, String host, String requestURI);
/**
* Make a PUT request
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientResponse
*/
Future<HttpClientResponse> put(int port, String host, String requestURI);
/**
* Make a DELETE request
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientResponse
*/
Future<HttpClientResponse> delete(int port, String host, String requestURI);
/**
* Make a HEAD request
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with HttpClientResponse
*/
Future<HttpClientResponse> head(int port, String host, String requestURI);
/**
* Create a WebSocket connection
* @param port Target port
* @param host Target host
* @param requestURI Request URI
* @return Future that completes with WebSocket
*/
Future<WebSocket> webSocket(int port, String host, String requestURI);
/**
* Close the client
* @return Future that completes when client is closed
*/
Future<Void> close();
/**
* Set connection handler
* @param handler Handler for new connections
* @return this for chaining
*/
HttpClient connectionHandler(Handler<HttpConnection> handler);
/**
* Set redirect handler
* @param handler Handler for redirects
* @return this for chaining
*/
HttpClient redirectHandler(Function<HttpClientResponse, Future<RequestOptions>> handler);
}Handle incoming HTTP requests with full access to headers, parameters, and body.
/**
* Incoming HTTP request to server
*/
interface HttpServerRequest extends ReadStream<Buffer> {
/**
* Get the HTTP method
* @return HTTP method
*/
HttpMethod method();
/**
* Get the request URI
* @return Request URI
*/
String uri();
/**
* Get the request path
* @return Request path
*/
String path();
/**
* Get the query string
* @return Query string
*/
String query();
/**
* Get query parameters as MultiMap
* @return Query parameters
*/
MultiMap params();
/**
* Get headers as MultiMap
* @return Request headers
*/
MultiMap headers();
/**
* Get form attributes (for form-encoded requests)
* @return Future that completes with form attributes
*/
Future<MultiMap> formAttributes();
/**
* Get the request body as Buffer
* @return Future that completes with body buffer
*/
Future<Buffer> body();
/**
* Get the request body as JsonObject
* @return Future that completes with JSON object
*/
Future<JsonObject> bodyAsJson();
/**
* Get the request body as JsonArray
* @return Future that completes with JSON array
*/
Future<JsonArray> bodyAsJsonArray();
/**
* Get the request body as String
* @return Future that completes with body string
*/
Future<String> bodyAsString();
/**
* Get the HTTP response for this request
* @return HttpServerResponse instance
*/
HttpServerResponse response();
/**
* Get remote address
* @return Remote socket address
*/
SocketAddress remoteAddress();
/**
* Get local address
* @return Local socket address
*/
SocketAddress localAddress();
/**
* Get HTTP version
* @return HTTP version
*/
HttpVersion version();
/**
* Check if connection is SSL
* @return true if SSL
*/
boolean isSSL();
/**
* Get the scheme (http/https)
* @return Scheme string
*/
String scheme();
/**
* Get the host header
* @return Host string
*/
String host();
/**
* Get cookies as map
* @return Cookie map
*/
Map<String, Cookie> cookieMap();
/**
* Set upload handler for file uploads
* @param handler Upload handler
* @return this for chaining
*/
HttpServerRequest uploadHandler(Handler<HttpServerFileUpload> handler);
/**
* Upgrade to WebSocket
* @return Future that completes with ServerWebSocket
*/
Future<ServerWebSocket> toWebSocket();
/**
* Check if WebSocket upgrade is expected
* @return true if WebSocket upgrade
*/
boolean isExpectMultipart();
/**
* Set expect multipart
* @param expect Whether to expect multipart
* @return this for chaining
*/
HttpServerRequest setExpectMultipart(boolean expect);
}Send HTTP responses with headers, status codes, and body content.
/**
* Outgoing HTTP response from server
*/
interface HttpServerResponse extends WriteStream<Buffer> {
/**
* Set the status code
* @param statusCode HTTP status code
* @return this for chaining
*/
HttpServerResponse setStatusCode(int statusCode);
/**
* Get the status code
* @return HTTP status code
*/
int getStatusCode();
/**
* Set the status message
* @param statusMessage HTTP status message
* @return this for chaining
*/
HttpServerResponse setStatusMessage(String statusMessage);
/**
* Get the status message
* @return HTTP status message
*/
String getStatusMessage();
/**
* Get response headers
* @return Response headers MultiMap
*/
MultiMap headers();
/**
* Get response trailers
* @return Response trailers MultiMap
*/
MultiMap trailers();
/**
* Write data to response
* @param chunk Data to write
* @return Future that completes when written
*/
Future<Void> write(Buffer chunk);
/**
* Write string to response
* @param chunk String to write
* @return Future that completes when written
*/
Future<Void> write(String chunk);
/**
* Write string with encoding to response
* @param chunk String to write
* @param enc Character encoding
* @return Future that completes when written
*/
Future<Void> write(String chunk, String enc);
/**
* End the response
* @return Future that completes when ended
*/
Future<Void> end();
/**
* End the response with data
* @param chunk Final data to write
* @return Future that completes when ended
*/
Future<Void> end(Buffer chunk);
/**
* End the response with string
* @param chunk Final string to write
* @return Future that completes when ended
*/
Future<Void> end(String chunk);
/**
* Send a file
* @param filename File to send
* @return Future that completes when file is sent
*/
Future<Void> sendFile(String filename);
/**
* Send a file with offset and length
* @param filename File to send
* @param offset Offset in file
* @param length Length to send
* @return Future that completes when file is sent
*/
Future<Void> sendFile(String filename, long offset, long length);
/**
* Close the response
* @return Future that completes when closed
*/
Future<Void> close();
/**
* Check if response is closed
* @return true if closed
*/
boolean closed();
/**
* Check if headers have been written
* @return true if headers written
*/
boolean headWritten();
/**
* Set body end handler
* @param handler Handler called when body ends
* @return this for chaining
*/
HttpServerResponse bodyEndHandler(Handler<Void> handler);
/**
* Add a cookie
* @param cookie Cookie to add
* @return this for chaining
*/
HttpServerResponse addCookie(Cookie cookie);
/**
* Remove a cookie
* @param name Cookie name to remove
* @return Cookie that was removed
*/
Cookie removeCookie(String name);
/**
* Set chunked transfer encoding
* @param chunked Whether to use chunked encoding
* @return this for chaining
*/
HttpServerResponse setChunked(boolean chunked);
/**
* Check if chunked
* @return true if chunked
*/
boolean isChunked();
}Create and send HTTP requests with full control over headers, body, and options.
/**
* Outgoing HTTP request from client
*/
interface HttpClientRequest extends WriteStream<Buffer> {
/**
* Get the HTTP method
* @return HTTP method
*/
HttpMethod method();
/**
* Get the URI
* @return Request URI
*/
String uri();
/**
* Get request headers
* @return Request headers MultiMap
*/
MultiMap headers();
/**
* Get authority (host:port)
* @return Authority string
*/
String authority();
/**
* Write data to request
* @param chunk Data to write
* @return Future that completes when written
*/
Future<Void> write(Buffer chunk);
/**
* Write string to request
* @param chunk String to write
* @return Future that completes when written
*/
Future<Void> write(String chunk);
/**
* End the request
* @return Future that completes when ended
*/
Future<Void> end();
/**
* End the request with data
* @param chunk Final data to write
* @return Future that completes when ended
*/
Future<Void> end(Buffer chunk);
/**
* End the request with string
* @param chunk Final string to write
* @return Future that completes when ended
*/
Future<Void> end(String chunk);
/**
* Send the request and get response
* @return Future that completes with response
*/
Future<HttpClientResponse> send();
/**
* Send the request with body
* @param body Request body
* @return Future that completes with response
*/
Future<HttpClientResponse> send(Buffer body);
/**
* Send the request with string body
* @param body Request body
* @return Future that completes with response
*/
Future<HttpClientResponse> send(String body);
/**
* Get the response future
* @return Future that completes with response
*/
Future<HttpClientResponse> response();
/**
* Get the underlying connection
* @return HTTP connection
*/
HttpConnection connection();
/**
* Set request timeout
* @param timeoutMs Timeout in milliseconds
* @return this for chaining
*/
HttpClientRequest setTimeout(long timeoutMs);
/**
* Set push handler for HTTP/2 server push
* @param handler Push handler
* @return this for chaining
*/
HttpClientRequest pushHandler(Handler<HttpClientRequest> handler);
/**
* Reset the request (HTTP/2)
* @param code Reset code
* @return Future that completes when reset
*/
Future<Void> reset(long code);
/**
* Set stream priority (HTTP/2)
* @param streamPriority Stream priority
* @return this for chaining
*/
HttpClientRequest setStreamPriority(StreamPriority streamPriority);
/**
* Get stream priority (HTTP/2)
* @return Stream priority
*/
StreamPriority getStreamPriority();
}Handle HTTP responses with access to status, headers, and body content.
/**
* Incoming HTTP response to client
*/
interface HttpClientResponse extends ReadStream<Buffer> {
/**
* Get the status code
* @return HTTP status code
*/
int statusCode();
/**
* Get the status message
* @return HTTP status message
*/
String statusMessage();
/**
* Get response headers
* @return Response headers MultiMap
*/
MultiMap headers();
/**
* Get response trailers
* @return Response trailers MultiMap
*/
MultiMap trailers();
/**
* Get cookies
* @return List of cookies
*/
List<String> cookies();
/**
* Get HTTP version
* @return HTTP version
*/
HttpVersion version();
/**
* Get the response body as Buffer
* @return Future that completes with body buffer
*/
Future<Buffer> body();
/**
* Get the response body as JsonObject
* @return Future that completes with JSON object
*/
Future<JsonObject> bodyAsJson();
/**
* Get the response body as JsonArray
* @return Future that completes with JSON array
*/
Future<JsonArray> bodyAsJsonArray();
/**
* Get the response body as String
* @return Future that completes with body string
*/
Future<String> bodyAsString();
/**
* Get the request that produced this response
* @return Original HttpClientRequest
*/
HttpClientRequest request();
/**
* Get the underlying net socket
* @return NetSocket if available
*/
NetSocket netSocket();
/**
* Set custom frame handler (HTTP/2)
* @param handler Custom frame handler
* @return this for chaining
*/
HttpClientResponse customFrameHandler(Handler<HttpFrame> handler);
}Full WebSocket client and server support with frame handling.
/**
* Base WebSocket interface
*/
interface WebSocket extends ReadStream<Buffer>, WriteStream<Buffer> {
/**
* Write binary message
* @param data Binary data
* @return Future that completes when written
*/
Future<Void> writeBinaryMessage(Buffer data);
/**
* Write text message
* @param text Text message
* @return Future that completes when written
*/
Future<Void> writeTextMessage(String text);
/**
* Write ping frame
* @param data Ping data
* @return Future that completes when written
*/
Future<Void> writePing(Buffer data);
/**
* Write pong frame
* @param data Pong data
* @return Future that completes when written
*/
Future<Void> writePong(Buffer data);
/**
* Set frame handler
* @param handler Frame handler
* @return this for chaining
*/
WebSocket frameHandler(Handler<WebSocketFrame> handler);
/**
* Set text message handler
* @param handler Text message handler
* @return this for chaining
*/
WebSocket textMessageHandler(Handler<String> handler);
/**
* Set binary message handler
* @param handler Binary message handler
* @return this for chaining
*/
WebSocket binaryMessageHandler(Handler<Buffer> handler);
/**
* Set pong handler
* @param handler Pong handler
* @return this for chaining
*/
WebSocket pongHandler(Handler<Buffer> handler);
/**
* Set close handler
* @param handler Close handler
* @return this for chaining
*/
WebSocket closeHandler(Handler<Void> handler);
/**
* Close the WebSocket
* @return Future that completes when closed
*/
Future<Void> close();
/**
* Close with code and reason
* @param statusCode Close status code
* @param reason Close reason
* @return Future that completes when closed
*/
Future<Void> close(short statusCode, String reason);
/**
* Check if SSL
* @return true if SSL
*/
boolean isSsl();
/**
* Get remote address
* @return Remote socket address
*/
SocketAddress remoteAddress();
/**
* Get local address
* @return Local socket address
*/
SocketAddress localAddress();
/**
* Get headers
* @return Headers MultiMap
*/
MultiMap headers();
}
/**
* Server-side WebSocket
*/
interface ServerWebSocket extends WebSocket {
/**
* Get the URI
* @return WebSocket URI
*/
String uri();
/**
* Get the path
* @return WebSocket path
*/
String path();
/**
* Get the query
* @return Query string
*/
String query();
/**
* Reject the WebSocket upgrade
* @param status Rejection status code
* @return Future that completes when rejected
*/
Future<Void> reject(int status);
/**
* Accept the WebSocket upgrade
* @return Future that completes when accepted
*/
Future<Void> accept();
/**
* Set handshake
* @param future Future to complete handshake
* @return this for chaining
*/
ServerWebSocket setHandshake(Future<Integer> future);
}Comprehensive configuration options for HTTP servers and clients.
/**
* HTTP Server configuration options
*/
class HttpServerOptions extends NetServerOptions {
HttpServerOptions setCompressionSupported(boolean compressionSupported);
HttpServerOptions setCompressionLevel(int compressionLevel);
HttpServerOptions setMaxWebSocketFrameSize(int maxWebSocketFrameSize);
HttpServerOptions setMaxWebSocketMessageSize(int maxWebSocketMessageSize);
HttpServerOptions setWebSocketSubProtocols(List<String> webSocketSubProtocols);
HttpServerOptions setHandle100ContinueAutomatically(boolean handle100ContinueAutomatically);
HttpServerOptions setMaxChunkSize(int maxChunkSize);
HttpServerOptions setMaxInitialLineLength(int maxInitialLineLength);
HttpServerOptions setMaxHeaderSize(int maxHeaderSize);
HttpServerOptions setMaxFormAttributeSize(int maxFormAttributeSize);
HttpServerOptions setDecompressionSupported(boolean decompressionSupported);
HttpServerOptions setAcceptUnmaskedFrames(boolean acceptUnmaskedFrames);
HttpServerOptions setInitialSettings(Http2Settings initialSettings);
HttpServerOptions setHttp2ConnectionWindowSize(int http2ConnectionWindowSize);
}
/**
* HTTP Client configuration options
*/
class HttpClientOptions extends NetClientOptions {
HttpClientOptions setKeepAlive(boolean keepAlive);
HttpClientOptions setMaxPoolSize(int maxPoolSize);
HttpClientOptions setMaxWaitQueueSize(int maxWaitQueueSize);
HttpClientOptions setHttp2MaxPoolSize(int http2MaxPoolSize);
HttpClientOptions setHttp2MultiplexingLimit(int http2MultiplexingLimit);
HttpClientOptions setHttp2ConnectionWindowSize(int http2ConnectionWindowSize);
HttpClientOptions setPipelining(boolean pipelining);
HttpClientOptions setPipeliningLimit(int pipeliningLimit);
HttpClientOptions setTryUseCompression(boolean tryUseCompression);
HttpClientOptions setMaxWebSocketFrameSize(int maxWebSocketFrameSize);
HttpClientOptions setMaxWebSocketMessageSize(int maxWebSocketMessageSize);
HttpClientOptions setDefaultHost(String defaultHost);
HttpClientOptions setDefaultPort(int defaultPort);
HttpClientOptions setMaxRedirects(int maxRedirects);
HttpClientOptions setForceSni(boolean forceSni);
HttpClientOptions setDecoderInitialBufferSize(int decoderInitialBufferSize);
HttpClientOptions setPoolCleanerPeriod(int poolCleanerPeriod);
HttpClientOptions setKeepAliveTimeout(int keepAliveTimeout);
}
/**
* HTTP methods enumeration
*/
enum HttpMethod {
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT, PATCH, OTHER
}
/**
* HTTP versions enumeration
*/
enum HttpVersion {
HTTP_1_0, HTTP_1_1, HTTP_2
}
/**
* WebSocket frame types
*/
enum WebSocketFrameType {
BINARY, TEXT, CLOSE, PING, PONG, CONTINUATION
}
/**
* Cookie interface
*/
interface Cookie {
String getName();
String getValue();
Cookie setDomain(String domain);
String getDomain();
Cookie setPath(String path);
String getPath();
Cookie setMaxAge(long maxAge);
Cookie setSecure(boolean secure);
boolean isSecure();
Cookie setHttpOnly(boolean httpOnly);
boolean isHttpOnly();
Cookie setSameSite(CookieSameSite sameSite);
CookieSameSite getSameSite();
String encode();
Cookie setPartitioned(boolean partitioned);
boolean isPartitioned();
}Basic HTTP Server:
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
String path = request.path();
HttpMethod method = request.method();
// Handle different paths
if (path.equals("/api/users") && method == HttpMethod.GET) {
request.response()
.putHeader("content-type", "application/json")
.end("{\"users\": []}");
} else if (path.equals("/api/users") && method == HttpMethod.POST) {
request.bodyAsJson().onSuccess(json -> {
// Process JSON body
request.response()
.setStatusCode(201)
.putHeader("content-type", "application/json")
.end("{\"id\": 123}");
});
} else {
request.response().setStatusCode(404).end("Not Found");
}
});
server.listen(8080).onSuccess(server -> {
System.out.println("HTTP server started on port 8080");
});HTTP Client Requests:
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpMethod;
Vertx vertx = Vertx.vertx();
HttpClient client = vertx.createHttpClient();
// GET request
client.get(80, "example.com", "/api/data")
.onSuccess(response -> {
System.out.println("Status: " + response.statusCode());
response.bodyAsString().onSuccess(body -> {
System.out.println("Body: " + body);
});
});
// POST request with JSON body
client.request(HttpMethod.POST, 80, "example.com", "/api/users")
.onSuccess(request -> {
request.putHeader("content-type", "application/json");
JsonObject user = new JsonObject()
.put("name", "John Doe")
.put("email", "john@example.com");
request.send(user.encode()).onSuccess(response -> {
System.out.println("User created: " + response.statusCode());
});
});WebSocket Server:
import io.vertx.core.http.ServerWebSocket;
HttpServer server = vertx.createHttpServer();
server.webSocketHandler(webSocket -> {
System.out.println("WebSocket connected: " + webSocket.path());
webSocket.textMessageHandler(message -> {
System.out.println("Received: " + message);
webSocket.writeTextMessage("Echo: " + message);
});
webSocket.closeHandler(v -> {
System.out.println("WebSocket disconnected");
});
});
server.listen(8080);File Upload Handling:
server.requestHandler(request -> {
if (request.path().equals("/upload") && request.method() == HttpMethod.POST) {
request.setExpectMultipart(true);
request.uploadHandler(upload -> {
String filename = upload.filename();
System.out.println("Uploading file: " + filename);
upload.streamToFileSystem("uploads/" + filename)
.onSuccess(v -> {
request.response()
.setStatusCode(200)
.end("File uploaded successfully");
})
.onFailure(err -> {
request.response()
.setStatusCode(500)
.end("Upload failed: " + err.getMessage());
});
});
}
});Install with Tessl CLI
npx tessl i tessl/maven-io-vertx--vertx-core