March 9, 2020
In this post I want to introduce the Jobby microframework for Kotlin and Java.
They don’t have a nice getting started site yet so I decided to make a quick write up.
We will write a small rest service in Kotlin for managing persons. This web service is going to take up under 30 lines of code.
Starting up
We will use Gradle to build our rest application.
mkdir kotlin_jooby
cd kotlin_jooby
gradle init --dsl kotlin
build.gradle.kts
plugins {
kotlin("jvm") version "1.3.0"
// to start our server directly from gradle
application
}
dependencies {
compile(kotlin("stdlib"))
compile("org.jooby:jooby:1.6.0")
// Add kotlin language support
compile("org.jooby:jooby-lang-kotlin:1.6.0")
// We need a server runtime to contain our application
runtime("org.jooby:jooby-netty:1.6.0")
}
repositories {
mavenCentral()
}
application {
mainClassName = "de.klg71.joobytest.MainKt"
}
We should be able to execute it without any error:
gradle build
Creating a microservice
As mentioned above we will create a small service for storing and retrieving person names.
Jooby has a very simple and intuitive DSL to define endpoints.
src/main/kotlin/de/klg71/joobytest/Main.kt
package de.klg71.joobytest
import org.jooby.Kooby
import org.jooby.run
// initial list of persons
val persons = mutableListOf("Max", "July", "Frank")
class App : Kooby({
// use the dsl to define our helloworld endpoint
get("/helloworld/") {
"Hello World!"
}
// list all persons currenyly available
get("/person"){
persons
}
// get a person by there position in the list
// we use {id} as the placeholder format and access it through the param variable
get("/person/{id}") {
persons[param("id").intValue()]
}
// post objects are accessed through the body method
// we dont have added jooby-jackson dependency though we cant parse JSON here
post("person") {
persons.add(body(String::class.java))
}
})
fun main(args: Array<String>) {
// startup the application
run(::App, *args)
}
Again we can start it with this gradle command:
gradle run
Now you should be able to reach the server on http://localhost:8080
.
Query the microservice
To connect to the microservice we will use the command line toold cURL .
At first we will test the helloworld endpoint:
> curl http://localhost:8080/helloworld
< Hello World!
Next we can list the available persons:
> curl http://localhost:8080/person
< [Max, July, Frank]
As you can recognize this is not valid jsob but only the toString
of the list.
To add a person we can just give this endpoint a name as payload:
> curl http://localhost:8080/person -d Kevin
< true
> curl http://localhost:8080/person
< [Max, July, Frank, Kevin]
We get true after sending the payload because this is what the list.add
returns and tells us, that the append was succesful.
At last we can test the single person endpoint with the new created Kevin:
> curl http://localhost:8080/person/3
< Kevin
Wrapping up
We used Jobby to create a super small service for persons. We created an rest endpoint in less than 30 lines of code.
Jobby feels pretty natural in this first experiment. The Kotlin DSL ist quite intuitive and aims for rapid prototyping.
I hope I can make a pull requests for a proper getting started page on their Github readme.
This small project is available on Github: jooby_kotlin