Exploring Javalin: A Lightweight Java Web Framework for Fast API Development
This article introduces Javalin, a lightweight Java web framework that supports WebSocket, HTTP/2, and asynchronous requests, demonstrates core code examples for routing, validation, and handlers, explains deployment with an embedded Jetty server, and resolves a common macOS port conflict.
Javalin is a lightweight Java web framework that supports WebSocket, HTTP/2, and asynchronous requests. It originated as a fork of SparkJava and was later influenced by the JavaScript framework koa.js, becoming an independent project.
Basic Example
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);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
// Pre‑handler (runs before every request)
app.before(ctx -> {
// code here
});
app.before("/path/*", ctx -> {
// code for specific path
});
// Endpoint handler
app.get("/", ctx -> {
ctx.json(object);
});
app.get("/hello/*", ctx -> {
// handle all /hello/* sub‑paths
});
// Post‑handler (runs after every request)
app.after(ctx -> {
// code here
});
app.after("/path/*", ctx -> {
// code for specific path
});The AccessManager interface can be used to implement authentication and authorization.
Deployment
To deploy a Javalin application, package a JAR with all dependencies (e.g., using the maven-assembly-plugin) and run it with java -jar filename.jar. Javalin includes an embedded Jetty server, so no external servlet container is required.
Port 7000 Conflict on macOS
When starting Javalin on macOS, the default port 7000 may be occupied by the system ControlCenter. The conflict can be identified with:
sudo lsof -nP -i4TCP | grep 7000
sudo ps aux | grep 1578Disabling the ControlCenter process resolves the issue (see the screenshot below).
References
Official documentation: https://javalin.io/documentation
Official tutorials: https://javalin.io/tutorials/
Running Javalin on GraalVM: https://javalin.io/2018/09/27/javalin-graalvm-example.html
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
