Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

What Javalin is

Javalin is a small web framework for Java and Kotlin. It starts an HTTP server right from the main method and lets you say in a few lines: "answer a GET /posts request with the list of posts". There are no annotations, no class scanning and no hidden magic: you see the whole path of a request in your own code. That makes Javalin a good first step into server-side Java.

How Javalin works

Inside Javalin runs Jetty, a proven web server written in Java. You do not install it separately: it comes as an ordinary Maven dependency and starts together with your program. Such a server is called embedded.

Application code consists of three kinds of parts:

PartWhat it does
RouteConnects an HTTP method and an address, such as GET /posts, to code
HandlerA lambda or method that receives the request and builds the response
Context ctxAn object with the request data and methods for the response

Javalin and big frameworks

JavalinSpring Boot, Quarkus
SizeOne libraryA platform of many modules
ConfigurationPlain Java codeAnnotations and settings files
Dependency injectionNone, you create objects yourselfBuilt in
Entry barrierLowHigher

Javalin does not hide how a web server works, so it is a convenient place to understand what a route, a response status or JSON is. Later this knowledge carries over to any other framework.

Version in this course

The course is written for Javalin 7.2 and Java 25. Javalin 7 needs Java 17 or newer and runs on Jetty 12. Version 7 changed the style: all routes are declared inside the configuration block Javalin.create(config -> ...). In older articles you will see calls like app.get(...) after the server has started — Javalin 7 does not have them.

Check that Java and Maven are installed:

java -version
mvn -version

Both commands should print versions: Java 25 (21 also works) and Maven 3.9.

What we will build

Throughout the course we build a REST API for a blog:

  1. /posts routes for listing, viewing, creating and deleting posts.
  2. JSON, data validation and clear errors.
  3. Controllers, static files, HTML templates, forms and file uploads.
  4. An H2 database through JDBC, password login with roles, CORS, cookies and sessions, API documentation in Swagger UI.
  5. A WebSocket chat, SSE notifications, tests, a build into a single jar file, settings from the environment and a request log.

We send requests to the API with the curl utility. The commands in the lessons are written for bash: Linux, macOS or Git Bash on Windows.

Common mistake

Maven runs the compiler of the Java that the JAVA_HOME variable points to. If an old version is still there, building the project from this course fails on the very first command:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.1:compile
(default-compile) on project blog: Fatal error compiling: error: release version 25 not supported

The Java 16 compiler does not know version 25. If you lower the version in the project, the error just moves to startup: UnsupportedClassVersionError ... (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 60.0 — the Javalin classes are built for Java 17. Install JDK 25 and make sure JAVA_HOME points to it.

Thanks for reading!

Donate to the site — we work hard for you.