Using Javalin with Kotlin to create a simple CRUD REST API

May 25, 2017
• Written by David ÅseReading time: 0-0 min
The source code for this tutorial can be found on
GitHub.
Please fork/clone and look while you read.

What You Will Learn

Setting up Kotlin with Maven

Creating a Javalin/Kotlin CRUD REST API (no database)

Some neat Kotlin features

The instructions for this tutorial will focus on IntelliJ IDEA,
as it’s made by JetBrains, the same people who make Kotlin.
We recommend downloading the free community edition
of IDEA while following this tutorial, but there is also Kotlin support in Eclipse.

Setting up Kotlin with Maven (in IntelliJ IDEA)

The good people over at JetBrains have an up-to-date
archetype
for Kotlin. To use it, do as follows:

File->New->Project

Maven->Create from archetype->org.jetbrains.kotlin:kotlin-archetype-jvm->Next

Follow the instructions and pick a project name

Create src/main/kotlin/app/Main.kt

There is no public static void main(String[] args) in Kotlin, instead you have a fun main(args: Array<String>).

funmain(args:Array<String>){println("Hello, world!")}

You'll have to point to the file (not class) containing this main function (not method)
from your pom.xml if you want to build a jar. Doing this is not necessary for this tutorial,
but the code on GitHub demonstrates how to do it for those interested.

The syntax (){} might look a little strange to Java programmers.
Kotlin supports trailing closures
and provides semicolon inference.
Simplified, this means you don’t have to wrap closures in parentheses and end statements with semicolons.

Creating a Javalin/Kotlin CRUD microservice

Kotlin data-classes

Kotlin has a really neat feature called
Data classes.
To create a data class you just have to write:

data classUser(valname:String,valemail:String,valid:Int)

… and you’re done! If you declare all parameters as val you get an immutable class similar to the
Lombok @Value annotation, only better.
Regardless of if you use var or val (or a mix) for your data class,
you get toString, hashCode/equals, copying and destructuring included:

valalice=User(name="Alice",email="[email protected]",id=0)valaliceNewEmail=alice.copy(email="[email protected]")// new object with only email changed
val(name,email)=aliceNewEmail// choose the fields you want
println("$name's new email is $email")// prints "Alice's new email is [email protected]"

Kotlin has type inference and named parameters (we could have written our arguments in any order).
It also has a nice standard library providing map-literal-like functions (so you won’t have to include guava in every project).

Creating a data access object

We need to be able to read out data somehow, so let’s set up some
basic CRUD functionality, with one added function for finding user by email:

The findByEmail function shows of some neat features. In addition to the
trailing closures that we saw earlier, Kotlin also has a very practical find function
and a special it keyword, which replaces user -> user style declarations with just it
(docs).
The function also demonstrates that == is the structural equality operator for Strings in Kotlin
(equivalent to .equals() in Java). If you want to check for referential equality in Kotlin you can use ===.
Another thing worth noticing is that the find-functions return User?, which means the function will
return either a User or null. In Kotlin you have to specify the possibility of a null-return.

Conclusion

I had only worked with Kotlin for a few hours before writing this tutorial,
but I’m already a very big fan of the language. Everything just seems to make sense, and the interoperability with Java is great.
IntelliJ will also automatically convert Java code into Kotlin if you paste it into your project.
Please clone the repo and give it a try!