An Introduction to Kotlin for server-side applications — Part-2

Sumit Sutar
Globant
Published in
3 min readMay 5, 2020
Photo by Andrew Neel on Unsplash

Welcome to part-2 of this series …!!!!

As we have seen in the 1st part of the series, It covers most of the theoretical concepts. In this article, we are going to build a small todo application. It will cover REST and JSON handling concepts.

We are going to use the MongoDB server to store the todo items in it. Alright, let’s get started. First, we will create a server and some routes.

We have created a server and introduced some routes. Routes are empty for now, but in the meantime, we will add life to it.

Now let’s create a model we require only one model in our application.

Basically model contains core application information. It includes data, validation rules, etc. and In our case, it will be the same therefore we have defined the fields which are required in our application.

So far we have created server, routes, and a model. Now, we will see the core part of the application. Which mostly contains business logic in it.

The above code is really straight forward we have just created simple methods that are returning something. Now, its time to connect our routes with the util function. We will return the request for now.

We have made some changes to the Main file. Added Lens to retrieve single and multiple todo items and the ID from the URL. Until now, we have just played around with the routes and utilities.

The fun part begins now.

Yes…!!! We are going to connect the application with the MongoDB server. You need to add below line to the Gradle file to make use of the MongoDB driver.

compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.12.1'

Now, let’s make changes in our util functions. You can refer to this link to get more information about the MongoDB driver.

Note: The documentation is mainly for JAVA but, you can refer it for Kotlin as well.

This is the thing I loved most about Kotlin. You can make use of plugin which is built for JAVA.

In the above piece of code, we have created a new instance, and using that instance we are connecting to the database and the required collection in the getCollection() method.

The getCollection() method created using the MongoClient instance so it has all the access of MongoDB APIs. We have called the API which is required to perform respective action.

Now let’s add some life to the routes so that we can go ahead and check whether the APIs are working or not.

That’s it we have created a todo application using Kotlin and the MongoDB. So we have covered the following points in this article.

  1. Created a real-life application using the Kotlin.
  2. Created REST APIs and we have seen how to handle the JSON.
  3. We have seen the interoperability feature of Kotlin in action making use of dependency that was built for JAVA only.

Here’s the Github repository link to this application.

Team Credits: Mukund | Nikhil Jadhav | Akshata Shelar | Archana

--

--