Javalin 1.0.0 stable is ready! - Javalin: Simple REST APIs for Java and Kotlin
source link: https://javalin.io/news/javalin-1.0.0-stable.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Introducing Javalin
Javalin is a very lightweight web framework for Kotlin and Java, inspired by Sparkjava and koa.js. Javalin is written in Kotlin with a few functional interfaces written in Java. This was necessary to provide an enjoyable and near identical experience for both Kotlin and Java developers.
Javalin is really more library than framework; you don’t need to extend or implement anything and there are very few “Javalin-concepts” you have to learn. Let’s look at some examples:
Hello World
- Kotlin
fun main(args: Array<String>) {
val app = Javalin.start(7000)
app.get("/") { ctx -> ctx.result("Hello World") }
}
API structure and server config
- Kotlin
val app = Javalin.create().apply {
enableStaticFiles("/public")
enableStandardRequestLogging()
port(port)
}.start()
app.routes {
path("users") {
get(UserController::getAllUserIds)
post(UserController::createUser)
path(":user-id") {
get(UserController::getUser)
patch(UserController::updateUser)
delete(UserController::deleteUser)
}
}
}
Filters and mappers
- Kotlin
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)
WebSockets
- Kotlin
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") }
}
Object mapping
- Kotlin
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.bodyAsClass(Array<Todo>::class.java)
ctx.status(204)
}
Uploads
- Kotlin
app.post("/upload") { ctx ->
ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) ->
FileUtils.copyInputStreamToFile(content, File("upload/" + name))
}
}
Kotlin and Java interoperability
The interoperability between Kotlin and Java is very important to Javalin. When moving a Javalin project from Java to Kotlin, you shouldn’t need to learn a new way of doing things. In the above examples you can switch between Kotlin and Java and see that everything is more or less the same, except for a few parens and semicolons. To maintain this consistent API for both languages is an important goal of the project.
Main features
- Lightweight - You don’t have to learn any advanced concepts to get started
- Consistent API - All handlers and mappers are void and operate on the Context (ctx)
- Almost identical API for both Kotlin and Java
- Not opinionated - A library rather than a framework (you never have to extend anything)
- Fully customizable embedded server (Jetty)
- JSON-object-mapping (via Jackson)
- Simple per-endpoint auth via
AccessManager
interface - Simple static file handling
- Lifecycle events
- CookieStore - An easy way to serialize and store objects in cookies
- Template rendering (Velocity, Freemarker, Thymeleaf, Mustache)
- Markdown rendering
Get involved
If you want to contribute to the project, head on over to GitHub.
No breaking changes since 0.5.0
If you’re currently using a 0.5.X version of Javalin, upgrading to 1.0.0 should not break anything.
Recommend
-
254
Javalin 0.5.1 released WebSockets (Sep 22, 2017) What's Javalin? Javalin is a very lightweight Java and Kotlin web framework which focuses on simplicity and Java/Kotlin interoperabi...
-
218
A lightweight Java and Kotlin web framework Well... the bad news is that the page you requested isn't he...
-
176
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, a...
-
132
A lightweight Java and Kotlin web framework Javalin 1.1.0 released GZIP, CORS and WebSocket improvements (Nov 18, 2017) What's J...
-
137
Javalin 1.2.0 released Custom jetty handlers (Dec 2, 2017) What's Javalin? Javalin is a very lightweight Java and Kotlin web framework which focuses on simplicity and Java/Kotlin in...
-
44
Introducing Javalin 2.0
-
48
Introducing Javalin 3.0 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 deve...
-
11
Simple 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 inter...
-
4
Test REST APIs with Kotlin, Retrofit, and JUnit5Test REST APIs with Kotlin, Retrofit, and JUnit5A common concern for any developer is whether their code works. Unit tests catch many simple errors but the only way to truly know whethe...
-
2
Foreword Javalin is a Java and Kotlin web framework which focuses on simplicity and Java/Kotlin interoperability. It supports WebSockets and HTTP3, and it uses Virtual Threads (from Project Loom) by default. Javalin aims to...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK