This is a very short tutorial
If you need to learn how to setup Kotlin with Maven, please follow the beginning of our Kotlin CRUD REST API tutorial
Dependencies
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin-bundle</artifactId>
<version>6.3.0</version>
</dependency>
Our main class
import io.javalin.http.staticfiles.Location
import io.javalin.http.bodyAsClass
data class Todo(val id: Long, val title: String, val completed: Boolean)
fun main() {
var todos = arrayOf(Todo(123123123, "My very first todo", false))
val app = Javalin.create {
it.staticFiles.add("/public", Location.CLASSPATH)
it.router.mount {
it.get("/todos") { ctx ->
ctx.json(todos)
}
it.put("/todos") { ctx ->
todos = ctx.bodyAsClass<Array<Todo>>()
ctx.status(204)
}
}
}.start(7070)
}
We’re use Javalin to serve our static files, as well as
handle two endpoints: get
and put
.
Most of the work here is being done by ctx.json
and ctx.bodyAsClass
,
which map a Todo data-class:
data class Todo(val id: Long = -1, val title: String = "", val completed: Boolean = false)
That’s it. The rest of the logic is in index.html
(vue template)
and todomvc.js
(vue logic).
This is not a JavaScript tutorial, so please have a look at those files for yourself.