5 Minutes : Let’s dive into server-side Kotlin!

Mert Bilgiç
HardwareAndro
Published in
5 min readAug 16, 2020

Hi everyone! In this article, we will engage with Kotlin and its server-side functionality. But before that, we need to know something about Kotlin. But I won’t mention about Kotlin and it’s history in this article. I’ll suppose you have some experience about Kotlin and know it’s history. If you don’t, let’s take you here and here. So, Let’s start 💻!

What are the benefits of Server-Side Kotlin?

In my opinion, Kotlin gives an awesome development experience. The best benefit of Kotlin is, it runs on JVM. So it makes things easier, because almost all of cloud providers support Java. If we need to order more benefits, Kotlin did it for us in here, but we’ll check again:

  • Expressiveness: Kotlin’s innovative language features, such as its support for type-safe builders and delegated properties, help build powerful and easy-to-use abstractions.
  • Scalability: Kotlin’s support for coroutines helps build server-side applications that scale to massive numbers of clients with modest hardware requirements.
  • Interoperability: Kotlin is fully compatible with all Java-based frameworks, which lets you stay on your familiar technology stack while reaping the benefits of a more modern language.
  • Migration: Kotlin supports gradual, step by step migration of large codebases from Java to Kotlin. You can start writing new code in Kotlin while keeping older parts of your system in Java.
  • Learning Curve: For a Java developer, getting started with Kotlin is very easy. The automated Java to Kotlin converter included in the Kotlin plugin helps with the first steps. Kotlin Koans offer a guide through the key features of the language with a series of interactive exercises.

Interested? Let’s continue;

Kotlin has some frameworks for creating well organized server-side applications. As I said before, because Kotlin runs on JVM, all Java frameworks support Kotlin now! Let’s check some,

1- Javalin ( GitHub )

2- Spring Boot ( GitHub )

3- Ktor ( We will use in this article ) ( GitHub )

4- Vertx ( GitHub )

You can check the frameworks with the links that I shared and you can give some stars to them on GitHub!

More interested? Let’s create a simple API!

We are going to create our API with Ktor. Before creating new project in Intellij IDEA, we need to install Ktor plugin. Open Intellij IDEA and open Plugins menu from Configure.

Now write “Ktor” in the search bar and install plugin. After install, you need to restart your IDE.

Reminder: Ktor doesn’t work on Android Studio!

Yay 🙌! You are ready to create Ktor projects now! Tap to “New Project” in Intellij IDEA and create a project.

Choose Ktor from left panel and press “Next” without changing any setting. You can change project name and group id, but I won’t show that.

Our Application.kt file when we create the project

Intellij IDEA creates the the project and makes the project runnable. You can see the initial Application.kt file above. We can run the project on the “localhost:8080” now, but when we run…

OH NO! 404!

We got an HTTP Error 404. This happens because we didn’t set any routes for our project. Routes are an installed feature to structure and simplify application request handling. Let’s set some routes!

Creating routes in Ktor is simple as this. Now when we enter “localhost:8080”, we will see “Hello, Kotlin!”.

What about POST Requests?

POST requests in Ktor are a little bit different -if we want to give JSON body-. Here, we need to install GSON dependency for Kotlin. To do that, in your build.gradle file, just add following implementation to “dependencies” part.

implementation “io.ktor:ktor-gson:$ktor_version”

And then edit your “Application.kt” file as following

Here, first we create a data class with a constant called name. We do it because we need to encode given JSON body. And DON’T FORGET to create data class on the top of your file, so GSON can see your data class.

We create our request with post() method, then we assign our body to postVal constant. Then we respond to this request with name given in body. Let’s test it with Postman.

So we created and sent the POST request successfully, so our API is growing :)

Let’s set file structure of our API

Now, we are separating routes from main Application file. For that, first we are going to create a file named “UsersRoute.kt” and paste the following code.

Also we are going to change our Application.kt file like following,

See? Now we don’t do any jobs in our main file, we separated our routes. Let’s test it 💻

When we send a GET request to “/users” endpoint, we saw the name of users in the array. Now let’s add an user.

For adding user, we need to send a POST request to “/users/add” endpoint like the following picture.

When the adding operation finishes successfully, we will get a response like “OK”. Let’s check whether “Veli” is added or not. We are sending GET request to “/users” again.

As we see, we successfully added “Veli” in the users array. Let’s greet “Veli”, all we need to do is, sending a POST request to “/users/greet” endpoint.

As we see, we got a response like “Hello, Veli”. So our greet request works very well, we are on the good way 🎉! We created REST API with Kotlin!

In this article, I shared something about Kotlin and it’s server-side functionality. If you like it, please clap the article and if you found it very informative, please share it. You can feel free to ask me your questions via my social media addresses. Have a nice day!

Project on GitHub: (Don’t forget to give a 🌟)

--

--