Configs2026年5月12日·1 分钟阅读

Javalin — Simple Lightweight Web Framework for Java and Kotlin

Javalin is a lightweight web framework for Java and Kotlin built on top of Jetty, designed for simplicity with a small learning curve and first-class support for both languages.

Introduction

Javalin is a lightweight web framework that runs on the JVM, targeting both Java and Kotlin developers. It wraps Jetty into a clean API where routes are defined as lambdas, WebSocket handlers are first-class, and OpenAPI documentation can be generated from code. It fills the gap between bare-metal servlet programming and heavyweight enterprise frameworks.

What Javalin Does

  • Creates HTTP and WebSocket endpoints with a simple lambda-based API
  • Runs on an embedded Jetty server with no external container required
  • Validates request data with built-in type-safe validators
  • Generates OpenAPI 3.0 specifications from annotated handlers
  • Supports both blocking and async request handling

Architecture Overview

Javalin is a thin layer over Jetty that translates servlet concepts into a simpler API. When a request arrives, it passes through a before-handler chain, matches against registered routes by method and path pattern, executes the handler, then runs after-handlers. The Context object wraps the servlet request and response, providing helpers for path parameters, query strings, JSON serialization (via Jackson or Gson), and file uploads.

Self-Hosting & Configuration

  • Add the Maven or Gradle dependency; Jetty is included transitively
  • Start a server with Javalin.create().start(port)
  • Configure the embedded Jetty via Javalin.create(config -> config.jetty.server(...))
  • Enable CORS with config.bundledPlugins.enableCors(cors -> cors.addRule(...))
  • Add the OpenAPI plugin for automatic Swagger UI at /swagger-ui

Key Features

  • First-class Kotlin support with extension functions and coroutine adapters
  • WebSocket and Server-Sent Events built into the core API
  • Plugin architecture for OpenAPI, SSL redirects, and rate limiting
  • Type-safe path parameter and query parameter extraction
  • Context-based API that avoids global state and thread-local patterns

Comparison with Similar Tools

  • Spark Java — similar micro approach but Java-only, less active development
  • Spring Boot — full enterprise framework with DI, far more setup for simple APIs
  • Ktor — Kotlin-native async framework by JetBrains, more idiomatic for pure Kotlin projects
  • Micronaut — compile-time DI and GraalVM support, steeper learning curve

FAQ

Q: Can Javalin be used with Kotlin coroutines? A: Yes. Javalin provides a coroutine plugin that lets you write suspend handlers and use structured concurrency.

Q: Does Javalin support GraalVM native images? A: Javalin can work with GraalVM, but Jetty requires reflection configuration. Community guides are available for setup.

Q: How does Javalin handle JSON serialization? A: It uses Jackson by default. You can swap in Gson or any other library by configuring a custom JSON mapper.

Q: Is Javalin suitable for microservices? A: Yes. Its small footprint, fast startup, and embedded server make it well suited for containerized microservices.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产