Explore Javalin: A Lightweight Java Backend Framework with WebSocket & HTTP2
Javalin is a lightweight Java web framework that supports WebSocket, HTTP/2, and asynchronous requests, offering simple configuration, route definitions, parameter validation, handlers, access management, and easy deployment via an embedded Jetty server, with examples and troubleshooting tips for common issues like port conflicts.
Javalin is a lightweight Java web framework. It supports WebSocket, HTTP/2 and asynchronous requests. It originated as a fork of SparkJava and was later influenced by the JavaScript framework koa.js.
Example: a more complex Hello World
var app = Javalin.create(config -> {
config.defaultContentType = "application/json";
config.autogenerateEtags = true;
config.addStaticFiles("/public");
config.asyncRequestTimeout = 10_000L;
config.dynamicGzip = true;
config.enforceSsl = true;
}).routes(() -> {
path("users", () -> {
get(UserController::getAll);
post(UserController::create);
path(":user-id", () -> {
get(UserController::getOne);
patch(UserController::update);
delete(UserController::delete);
});
ws("events", userController::webSocketEvents);
});
}).start(port);Path parameter validation
var myQpStr = ctx.queryParam("my-qp"); // returns string or null
var myQpInt = ctx.pathParam("my-qp", Integer.class).get(); // returns int or throws
var myQpInt = ctx.formParam("my-qp", Integer.class).check(i -> i > 4).get(); // int > 4
// validate dependent query parameters
var fromDate = ctx.queryParam("from", Instant.class).get();
var toDate = ctx.queryParam("to", Instant.class)
.check(it -> it.isAfter(fromDate), "'to' has to be after 'from'")
.get();
// validate JSON body
var myObject = ctx.bodyValidator(MyObject.class)
.check(obj -> obj.myObjectProperty == someValue)
.get();Handlers
// before handler
app.before(ctx -> {
// runs before all requests
});
app.before("/path/*", ctx -> {
// runs before /path/* requests
});
// endpoint handler
app.get("/", ctx -> {
// some code
ctx.json(object);
});
app.get("/hello/*", ctx -> {
// captures all requests under /hello/
});
// after handler
app.after(ctx -> {
// runs after all requests
});
app.after("/path/*", ctx -> {
// runs after /path/* requests
});AccessManager can be used for authentication and authorization.
To deploy a Javalin application, package it into a jar (e.g., using the maven-assembly-plugin) and run java -jar filename.jar. Javalin includes an embedded Jetty server, so no external application server is required.
Javalin provides educational pages highlighting its benefits for students, and a series of tutorials such as “Running on GraalVM” and “Kotlin CRUD REST API”. Documentation and downloads are available via Maven Central.
Deployment execution
Running mvn package creates a jar that can be started with java -jar xxx.jar.
Port 7000 conflict on macOS
When starting Javalin on macOS, the default port 7000 may be occupied. Use lsof -nP -i4TCP | grep 7000 to identify the process, then ps aux | grep 1578 to find the owning process. In this case, the system's ControlCenter was using the port; disabling it resolves the issue.
References: official documentation (https://javalin.io/documentation), tutorials (https://javalin.io/tutorials/), and the GraalVM example.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
