The project webpage is javalin.io.
Documentation: javalin.io/documentation
Chatroom: https://gitter.im/javalin-io/general
Contributions are very welcome: CONTRIBUTING.md
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>2.1.0</version>
</dependency>
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"));
}
}
compile 'io.javalin:javalin:2.1.0'
import io.javalin.Javalin
fun main(args: Array<String>) {
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).
val app = Javalin.create().apply {
enableCorsForAllOrigins()
enableStaticFiles("/public")
enableStaticFiles("uploads", Location.EXTERNAL)
}.start(port)
app.routes {
path("users") {
get(UserController::getAll)
post(UserController::create)
path(":user-id") {
get(UserController::getOne)
patch(UserController::update)
delete(UserController::delete)
}
}
}
app.ws("/websocket") { ws ->
ws.onConnect { session -> println("Connected") }
ws.onMessage { session, message ->
println("Received: " + message)
session.remote.sendString("Echo: " + message)
}
ws.onClose { session, statusCode, reason -> println("Closed") }
ws.onError { session, throwable -> 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)
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<Array<Todo>>()
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