by Adam Arold
Going Beyond Android: how Kotlin works on the Backend
This article is part of a series.
While most developers use Kotlin on Android, it is also a viable option on other platforms. In this article, we’ll look at how it works on the backend.
As I have written about this before, I think that the interop between Java and Kotlin is quite seamless. This also means that using Kotlin in place of Java on the backend is rather easy. Apart from a few nuisances, you can pretty much start writing your new features in Kotlin within your Java project. Or if you just want to try it out, you can start by writing your tests with it.
If you look around, it seems that companies with a big slice of the backend pie also have the same thought: the new version of Spring has some features dedicated to Kotlin, and you can even use Kotlin to write your Gradle scripts using the kotlin-dsl.
What is interesting to note here is that you don’t need Kotlin support for any of these libraries, because the Java interop features of Kotlin are so good.
When you start to work with Kotlin on the backend, you have several options at your disposal.
If you choose to use a library written in Kotlin, you get some major advantages: there will be no compatibility issues related to the language you use since everything is written in Kotlin. Another thing worth mentioning is that there are some things which are only present in Kotlin, such as Coroutines and reified generics.
The trade-off is that most of these libraries are not very old and might have the typical problems with new projects: lack of documentation and eventual bugs or design issues.
If you need something that is battle-tested, you can’t go wrong with tools like Spring or Mockito. What you get with these is that most issues are well-documented, and you’ll get answers for your questions pretty quickly. What you lose, though, is some of the nice things about Kotlin: they don’t have Kotlin DSLs, and the code will be quite Java-ish.
Another option is to pick a library which has built-in support for Kotlin. RxKotlin and vert.x are good examples of this. I’ll call these projects hybrids from now on. With them you get the best of both worlds: you will usually have a good API which is idiomatic from a Kotlin perspective, and behind it there will be something which is well-known and battle-hardened.
Let’s look at some libraries which you might want to try out.
Note that the following are just my opinions, and as such they are subjective.
Ktor is one of the newer web frameworks written in Kotlin. It comes with embedded Netty and a nice DSL to boot. What is interesting in this one is that it takes advantage of Kotlin’s Coroutine support. This is how it looks in practice:
I was concerned when I tried it out that the documentation is quite lacking. When you bump into a problem, you are more likely to get stuck. It also does not perform well, and interop with Java is a bit sketchy.
Javalin is an other web framework which has a very simple, fluent, and readable API. It can also work with multiple embedded web servers like Netty or Undertow. What I liked most is that it strikes a balance between the minimalistic approach of Spark (not to be confused with Apache Spark) and the low-level nature of vert.x. The documentation is also very good so you can get started in no time:
I like that the API is a little more Java-ish than Ktor’s, so if you come from a Java background it might be easier to get started with.
Hexagon is an interesting choice for writing web applications. The name choice is not random: it encourages using the hexagonal architecture (more commonly known as clean architecture), and it is also more performant than ktor or even the Spring framework! The DSL which you get is also pretty descriptive:
Off the web
It is worth noting that there are a lot of tools written in Kotlin which you can pick from. Kotlin has a very useful specification framework. If you don’t like the fact that there are no good HTTP clients for Java, you can now take advantage of Fuel which is a very handy tool for interfacing with REST endpoints and beyond. You even have tools for writing games in Kotlin.
vert.x is a multi-module web framework akin to Spring. What is important to note here is that there is a documentation section dedicated to Kotlin. vert.x might be your choice for writing web applications if performance is paramount to you: vert.x has very good benchmark scores. This should not be a surprise, since it implements the multi reactor pattern (the reactor pattern might be familiar to you from node.js).
It is worth noting that there are very good Kotlin examples for vert.x. You’ll need them because it has some concepts which are not present elsewhere and setting it up is also a bit more involved:
For a lot of Java developers, Spring is the de facto tool for writing Java applications. The good news is that since 5.0 Spring has built-in support for Kotlin. There is a whole plethora of Spring projects which you can pick from. If you are interested, there is a simple tutorial which will get you started using Spring with Kotlin. Here is how Hello World looks with it:
Sparkjava is a minimalistic (micro)web framework. You can get started with it with practically zero time investment, and if you come from node.js it is also a very good choice. You also can’t get more minimal than this:
While interfacing with Java tools is usually pretty convenient, some of the tools above have tooling support written for Kotlin:
vertx-lang-kotlin provides useful Kotlin-specific options for vert.x like coroutines, one-shot workers, or reactive streams.
While using Mockito from Kotlin is mostly pleasant, mockito-kotlin improves upon that by giving you a nice DSL and taking care of some issues.
A working example
Now let’s look at a step-by-step example. We’ll use Spring Boot with Spring Initializr.
The source code of this tutorial can be found here.
Spring Boot comes with Spring Initializr, which is a handy tool with which you can quickly kick off your project. I recommend selecting Gradle Project with Kotlin and Spring Boot 2.0.0 for this example, since with 2.0.0 you’ll get the features of Spring 5.0.
If you click “Switch to full version” you’ll also be able to piece together a fine-grained skeleton. There is a cornucopia of topics from which you can pick tools ranging from Web to AWS and more. For this exercise I picked Web only.
After setting up Initializr, click “Generate Project” and open it in your IDE. You might notice that compared to a simple Java project, you don’t have to add much to your
build.gradle which is Kotlin-specific. I’ve extracted them in this example:
Adding a simple Controller
If you look at the entry point of the application, it is rather minimalistic:
Now the only thing we need for a working Hello World is to add a
RestController to our project:
and Bam! you are done! You can go and check out the result at
http://localhost:8080/ after starting it up with
./gradlew bootRun :
In this article, we have explored some of the more well-known options for backend development with Kotlin. We have also seen that prominent actors on the market have embraced Kotlin, and backend development can be a lot simpler with it compared to pure Java.
Thanks for reading! You can read more of my articles on my blog.