- About Javalin
- Philosophy
- API design
- Java and Kotlin interoperability
- Contact
- Why Javalin?
- Lightweight
- Interoperable
- Flexible
- Educational
- OpenAPI
- Jetty
- Our sponsors
- Declare server and API in the same place
- An active community
- Who’s using Javalin?
- What is Javalin used for?
- Saved searches
- Use saved searches to filter your results more quickly
- License
- javalin/javalin
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
About Javalin
Javalin started as a fork of the Java and Kotlin web framework Spark, but quickly turned into a ground-up rewrite influenced by koa.js. Both of these web frameworks are inspired by the modern micro web framework grandfather: Sinatra, so if you’re coming from Ruby then Javalin shouldn’t feel too unfamiliar.
Philosophy
Like Sinatra, Javalin is not aiming to be a full web framework, but rather just a lightweight REST API library (or a micro framework, if you must). There is no concept of MVC, but there is support for template engines, WebSockets, and static file serving for convenience. This allows you to use Javalin for both creating your RESTful API backend, as well as serving an index.html with static resources (in case you’re creating an SPA). This is practical if you don’t want to deploy an apache or nginx server in addition to your Javalin service. If you wish to use Javalin to create a more traditional website instead of a REST APIs, there are several template engine wrappers available for a quick and easy setup.
API design
All the methods on the Javalin instance return this , making the API fully fluent. This will let you create a declarative and predictive REST API, that will be very easy to reason about for new developers joining your project.
Java and Kotlin interoperability
Javalin is both a Kotlin web framework and a Java web framework, meaning the API is being developed with focus on great interoperability between the two languages. The library itself is written primarily in Kotlin, but has a few core classes written in Java to achieve the best interoperability between the two languages. Javalin is intended as a “foot in the door” to Kotlin development for companies that already write a lot of Java.
When moving a Javalin project from Java to Kotlin, you shouldn’t need to learn a new way of doing things. To maintain this consistent API for both languages is an important goal of the project.
Contact
If you want to get in touch, please create an issue on our GitHub tracker.
Javalin is a Java and Kotlin web framework, open sourced under the Apache 2 license | Download | About | News | Contact | For educators
Why Javalin?
Unlike other Java and Kotlin web frameworks, Javalin has very few concepts that you need to learn. You never extend classes and you rarely implement interfaces.
Lightweight
Javalin is just a few thousand lines of code on top of Jetty, and its performance is equivalent to raw Jetty code. Due to its size, it’s very easy to reason about the source code.
Interoperable
Other Java and Kotlin web frameworks usually offer one version for each language. Javalin is being made with inter-operability in mind, apps are built the same way in both Java and Kotlin.
Flexible
Javalin is designed to be simple and blocking, as this is the easiest programming model to reason about. But, if you set a Future as a result, Javalin switches into asynchronous mode.
Educational
Visit our educators page if you’re teaching web programming and looking for a web framework which will get out of your way and let you focus on the core concepts of your curriculum.
OpenAPI
Many lightweight Java and Kotlin web frameworks don’t support OpenAPI, but Javalin does (including Swagger UI and ReDoc). Learn more at the OpenAPI plugin page.
Jetty
Javalin runs on top of Jetty, one of the most used and stable web-servers on the JVM. You can configure the Jetty server fully, including SSL and HTTP3 and everything else that Jetty offers.
Our sponsors
https://featureupvote.com
Declare server and API
in the same place
import io.javalin.Javalin; import static io.javalin.apibuilder.ApiBuilder.*; Javalin app = Javalin.create(config -> config.enableCorsForAllOrigins(); config.asyncRequestTimeout = 10_000L; config.enforceSsl = true; >).routes(() -> path("users", () -> get(UserController::getAll); post(UserController::create); path("", () -> get(UserController::getOne); patch(UserController::update); delete(UserController::delete); >); ws("events", userController::webSocketEvents); >); >).start(port);
import io.javalin.Javalin; import io.javalin.apibuilder.ApiBuilder.*; val app = Javalin.create config -> config.enableCorsForAllOrigins() config.asyncRequestTimeout = 10_000L config.enforceSsl = true >.routes path("users") get(UserController::getAll) post(UserController::create) path("") get(UserController::getOne) patch(UserController::update) delete(UserController::delete) > ws("events", userController::webSocketEvents) > >.start(port)
Creating a REST API has never been easier
An active community
Javalin 1.0 was released in 2017, and has been in steady development since.
As of April 2022, the project consists of less than 7000 lines of code with more than 10 000 lines of tests. It has more than a hundred contributors, more than four hundred forks, and more than five thousand stars on GitHub. The project has around 300 000 downloads per month, and has been released 101 times in five years (about two times per month).
Who’s using Javalin?
What is Javalin used for?
Check out our recent survey results
to learn more about how people use Javalin.
Javalin is a Java and Kotlin web framework, open sourced under the Apache 2 license | Download | About | News | Contact | For educators
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A simple and modern Java and Kotlin web framework
License
javalin/javalin
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Javalin — A simple web framework for Java and Kotlin
Javalin is a very lightweight web framework for Kotlin and Java which supports WebSockets, HTTP2 and async requests. Javalin’s main goals are simplicity, a great developer experience, and first class interoperability between Kotlin and Java.
Javalin is more of a library than a framework. Some key points:
- You don’t need to extend anything
- There are no @Annotations
- There is no reflection
- There is no other magic; just code.
- ❤️ Sponsor Javalin
- The project webpage is javalin.io (repo for webpage is at github.com/javalin/javalin.github.io).
- Documentation: javalin.io/documentation
- Chat on Discord: https://discord.gg/sgak4e5NKv
- Chat on Slack: https://join.slack.com/t/javalin-io/shared_invite/zt-1hwdevskx-ftMobDhGxhW0I268B7Ub~w
- Contributions are very welcome: CONTRIBUTING
- License summary: https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
- Interesting issues: /tipsy/javalin/issues?q=label:INFO
dependency> groupId>io.javalingroupId> artifactId>javalinartifactId> version>5.6.1version> dependency>
implementation "io.javalin:javalin:5.6.1"
import io.javalin.Javalin; public class HelloWorld < public static void main(String[] args) < Javalin app = Javalin.create().start(7000); app.get("/", ctx -> ctx.result("Hello World")); > >
import io.javalin.Javalin fun main() < val app = Javalin.create().start(7000) app.get("/") < ctx -> ctx.result("Hello World") > >
This section contains a few examples, mostly just extracted from the docs. All examples are in Kotlin, but you can find them in Java in the documentation (it’s just syntax changes).
Api structure and server config
val app = Javalin.create < config -> config.defaultContentType = "application/json" config.autogenerateEtags = true config.staticFiles.add("/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)
app.ws("/websocket/:path") < ws -> ws.onConnect < ctx -> println("Connected") > ws.onMessage < ctx -> val user = ctx.messageUser>(); // convert from json string to object ctx.send(user); // convert to json string and send back > ws.onClose < ctx -> println("Closed") > ws.onError < ctx -> println("Errored") > >
app.before("/some-path/*") < ctx -> ... > // runs before requests to /some-path/* app.before < ctx -> ... > // runs before all requests app.after < ctx -> ... > // runs after all requests app.exception(Exception.class) < e, ctx -> ... > // runs if uncaught Exception app.error(404) < ctx -> ... > // runs if status is 404 (after all other handlers) app.wsBefore("/some-path/*") < ws -> ... > // runs before ws events on /some-path/* app.wsBefore < ws -> ... > // runs before all ws events app.wsAfter < ws -> ... > // runs after all ws events app.wsException(Exception.class) < e, ctx -> ... > // runs if uncaught Exception in ws handler
var todos = arrayOf(...) app.get("/todos") < ctx -> // map array of Todos to json-string ctx.json(todos) > app.put("/todos") < ctx -> // map request-body (json) to array of Todos todos = ctx.bodyArrayTodo>>() ctx.status(204) >
app.post("/upload") < ctx -> ctx.uploadedFiles("files").forEach < (contentType, content, name, extension) -> FileUtil.streamToFile(content, "upload/$name") > >
- Blake Mizerany, for creating Sinatra
- Per Wendel, for creating Spark
- Christian Rasmussen, for being a great guy
- Per Kristian Kummermo, also for being a great guy
About
A simple and modern Java and Kotlin web framework