Need help with javalin?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

tipsy
4.6K Stars 396 Forks Apache License 2.0 1.3K Commits 55 Opened issues

Description

A simple and modern Java and Kotlin web framework

Services available

!
?

Need anything else?

Contributors list

# 3,376
play-fr...
Kotlin
javalin
html5
1030 commits
# 87,287
Kotlin
javalin
React
react-c...
27 commits
# 112,042
Kotlin
Applica...
SQL
javalin
15 commits
# 90,456
Kotlin
Shell
mvi-arc...
kotlin-...
13 commits
# 111,024
html5
Kotlin
javalin
servlet
13 commits
# 147,976
Kotlin
HTML
javalin
servlet
9 commits
# 155,269
gtk
Linux
Kotlin
javalin
8 commits
# 174,533
faceboo...
Google
Kotlin
HTML
6 commits
# 154,977
Shell
fp
Kotlin
javalin
6 commits
# 188,196
Kotlin
HTML
javalin
servlet
5 commits
# 188,308
Kotlin
HTML
javalin
servlet
5 commits
# 187,283
HTML
Kotlin
javalin
servlet
5 commits
# 193,669
Kotlin
HTML
javalin
servlet
4 commits
# 173,300
scienti...
JavaFX
Shell
Kotlin
4 commits
# 173,940
Shell
MATLAB
yolov2
Kotlin
4 commits
# 189,791
Kotlin
HTML
javalin
servlet
4 commits
# 30,875
Angular
TypeScr...
Webpack
angular...
4 commits
# 200,374
C++
Kotlin
Shell
javalin
4 commits
# 227,150
Kotlin
HTML
javalin
servlet
3 commits
# 227,443
Kotlin
HTML
javalin
servlet
3 commits

Chat at https://gitter.im/javalin-io/general Chat at https://discord.gg/sgak4e5NKv Travis License Maven

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.

General information: * The project webpage is javalin.io (repo for webpage is at github.com/javalin/javalin.github.io). * Documentation: javalin.io/documentation * Chat on Gitter: https://gitter.im/javalin-io/general * Chat on Discord: https://discord.gg/sgak4e5NKv * Contributions are very welcome: CONTRIBUTING.md * License summary: https://tldrlegal.com/license/apache-license-2.0-(apache-2.0) * Interesting issues: /tipsy/javalin/issues?q=label:INFO

Quickstart

Add dependency

Maven

    io.javalin
    javalin
    3.13.3

Gradle

compile "io.javalin:javalin:3.13.3"

Start programming (Java)

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")); } }

Start programming (Kotlin)

import io.javalin.Javalin

fun main() { val app = Javalin.create().start(7000) app.get("/") { ctx -> ctx.result("Hello World") } }

Examples

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.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)

WebSockets

app.ws("/websocket/:path") { ws ->
    ws.onConnect { ctx -> println("Connected") }
    ws.onMessage { ctx ->
        val user = ctx.message(); // 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") }
}

Filters and Mappers

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

JSON-mapping

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.body>()
    ctx.status(204)
}

File uploads

app.post("/upload") { ctx ->
    ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) ->
        FileUtil.streamToFile(content, "upload/$name")
    }
}

OpenAPI (Swagger)

Javalin has an OpenAPI (Swagger) plugin. Documentation can be enabled both through a DSL and through annotations, and Javalin can render docs using both SwaggerUI and ReDoc. Read more at https://javalin.io/plugins/openapi.

Special thanks

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.