Build Restful web services with Spring boot and kotlin.

Khalid Abdul Wahab
Hacktive Devs
Published in
5 min readFeb 18, 2019

In recent times, Kotlin has gained a lot of popularity due to the many awesome problems it solves for java developers and also its first class support on Android.

Due to its popularity, Spring framework 5(A server side framework) also introduced support for kotlin in building web applications.

In this article, we will build a Restful CRUD(“create, read , update and delete”) API for a simple journal app.We will be making use of kotlin, Spring Boot 2.x , which is based on Spring framework 5, MySql for the database and Jpa & Hibernate to access our data from the database. lets get started!!

Boot strapping our application

We are going to be using spring initializr to boot strap our application. Let us go to spring initializr , enter your preferred artifact and group, select a maven project , select kotlin as the language to use, select at least spring boot version 2.x , add the dependencies for web, Jpa and Mysql, then you click on generate project, download the zip file and extract the zip file to your workspace. You can then import the maven project created for you to eclipse , intelli j or any other IDE you may be using.

Now that we have created our application, we can look at our pom.xml file and check if all the dependencies were added. Note that i added only the dependencies i’ll be needing for this article, you could add any other dependencies you need on the spring initializr page or by just adding it to your pom.xml file. Here is how my pom.xml file looks like

You can see from the pom.xml file that a few kotlin dependencies have been added for us by the spring initializr.

Setting up the database

Next step is to set up the database url in the application.properties file of the project.

Make sure you change the username and password based on your database configuration. Also notice that i added the url to my database server. You can set up your database server by using mySql workbench or on your command line. You can check here for instructions on how to set up mySql workbench or here if you want to set it up from the command line.

Now we need to make sure that the root class containing the main function is open. This is necessary because by default, all classes in kotlin are final and cannot be inherited from. For the spring framework however, classes annotated with @springbootapplication, @configuration etc need to be declared non final and this is the case for the root application class. Making this class open is easy, all we need to do is to add the open keyword to the class declaration. Here is an example

In a large project however, you might not want to add the open keyword to every class with an annotation that requires a non final class as this may be tedious. Luckily for us, from kotlin 1.0.6 , there is a plugin to automate this process, You can check that out here .

Siiggghhhhh!! After all that set up, we can now finally create some entities!

Creating the entity

Suppose we have a table called Journals, each journal has an id, title and content. Here is how that will look like in kotlin

If we take a look at the Journal class, we notice that we annotated the class as an entity with the table name, and it is declared as a data class. A data class will automatically generate equals(), hashcode(), toString() and copy() methods for us. Also, notice that the id is created using the identity type strategy. This will make sure the id is automatically generated for us. Note also that the fields make use of default constructor values, This is because hibernate needs to instantiate this class with an empty constructor. Using default values here makes hibernate instantiate the class without any arguments being passed in. Notice also how concise the code looks, well, that’s because kotlin is awesome! :)

Creating the repository

Every spring rest api makes use of a repository. The repository interface helps us generate CRUD Methods automatically just by extending jpa repository class from the interface we create.

This interface just extends the jpaRepository class and passes in the journal and the data type of the id for the journal table and that’s it for the repository interface.

Creating the controller

The controller class consists of the endpoints for creating, updating, deleting and reading from the database. Here is what the controller class looks like

Notice that we annotated the class as a restcontroller here rather than just a controller since we are building a rest api. Also For each endpoint, kotlin’s functional style is used in order to reduce boiler plate code and to make the code more concise. Note also that we autowired the journal repository interface to the controller so that we can call CRUD methods that the jpa repository has created for us behind the scenes.

That’s it! we can now run our application and test our endpoints!

We can test our endpoints using postman(A complete api development environment which can be used to test api’s) or on our browser

post request
post reponse

Conclusion

We can see from this example that kotlin makes spring boot even easier and reduces a lot of boiler plate code we would have had to write if we were using Java.

All code samples can be found in my github repository. Make sure you star the repository as well. Thank you for reading :)

--

--

Khalid Abdul Wahab
Hacktive Devs

Android developer | Backend web developer. I love to share the little i know