Kotlin in the backend, Spring with Kotlin

Rodrigo Silva
6 min readMar 23, 2021

--

For those of you who don’t know, Kotlin is a statically typed programming language designed by Jetbrains, it’s the official language for the development of native android applications, but what you may not know is that you can use Kotlin to develop your server-side applications.

There are many libraries and frameworks which allow you to create your backend using Kotlin, such as Ktor and Spring.

In this article, we will take a look into using Kotlin together with the Spring framework.

So What is Spring?

Spring is an application framework and inversion of control container(IOC) for the Java platform, however, Spring also allows the developer to use languages such as Groovy, Kotlin and many others.

How to Create a Spring Application using Kotlin?

In this article, we will develop a simple web API using Spring Web, a bookshop, which allows to query for all books, create, update and delete them. For that, we will use Spring Initializr to generate our project with the necessary dependencies.

Spring Initializr

Select Maven or Gradle(I prefer Gradle), Kotlin, add a dependency to Spring Web. Next, fill in the information in the Project Metadata area like the picture above.

After generating the project, extract the contents of the zip file to a destination folder of your choice. After that open the project in your IDEA of choice, I’ll use IntelliJ IDEA.

After building your project, the structure of your project should be similar to the one presented below.

Project Structure

Create Entities and Endpoints

For the simplicity of the exercise, our book entity will only have a unique ISBN and the books name, as for endpoints, we want as many endpoints as to:

  • Get the information about all books;
  • Get the information of a specific book (using its unique ISBN);
  • Create a book (providing an ISBN and name);
  • Update the book’s name(in case of an error in the name);
  • Delete a specific book.

We’ll also need the define a DTO for the Book and Collection<Book>, DTO stands for Data Transfer Object, it is a representation of a specific entity more fitting for transferring it through software subsystems and the network(marshalling and unmarshalling). Let’s call it BookDto and BooksDto, for this example the representation is the same as a Book, however, it’s a good practice to separate what’s used in our backend and what’s sent through the network to the API clients.

Book Class
BookDto’s used in GET’s
BookDto’s used in POST and PUT

So now we have defined with success our entity and our DTO’s, it’s time to define our controller, a controller in Spring is where we store the code we want to be executed when a request to an endpoint is made.

Book Controller

The BookController class is where we’ll write code to handle each endpoint call, as seen before, we will make available 5 endpoints:

  • GET /books Get information about all books;
  • GET /books/{isbn} Get the information of a specific book (using its unique ISBN);
  • POST /books Create a book (providing a ISBN and name);
  • PUT /books/{isbn} Update the book’s name(in case of error in the name);
  • DELETE /books/{isbn} Delete a specific book.

For each endpoint, we must create a specific method.

Get all Books and Get specific Book
Create and update Book
Delete Book

bookService is an instance of BookService, this class is where the business logic is stored.

Book Service

Why store business logic elsewhere? Well, if our application moves from let’s say REST to GRPC, our business logic layer and the ones below don’t need to change, only the presentation layer(controller) needs to be replaced.

Like the Book Controller, our BookService will also have a method for each operation, i.e. GetAllBooks, CreateBook, etc…)

Our BookService uses an instance of BookRepository in order to persist state about the books created, for now, we are using an in-memory map, but we can later change it to use a relational or non-relational database. This is the great thing about using a layered architecture, we just have to define the interfaces and we don’t need to “worry” about the implementation.

We will use withContext(Dispatchers.Default) to start a coroutine, we can also simulate a delay using the delay function(…)

Book Service

Needless to say, our service does not use transactions since we aren’t using a database, but we aren’t also taking into account concurrency between books with the same ISBN, this should be taken into account, but since this is a simple example I won’t be addressing concurrency control.

Book Repository

As stated before, to maintain state in our BookStore API we need a single place where we can request and store information about our books, this is where the BookRepository comes into play, this class is used as a storage for our books, it makes available an interface to manage books.

An in-memory map is used, using as a key the ISBN of a book and the value is the instance of the book itself, of course, in a real system we wouldn’t use a map to store our information, since it’s volatile, if we restart the application, all information will be lost, to persist information, one should use a database.

Book Repository

Testing our endpoints

To test our endpoints we will use a tool called Postman(Not sponsored), it’s the tool which I like to use the most when testing Web API’s, it allows the user to store all requests into collections, which is great for organizing and sharing with colleagues and friends.

Get All books — Empty
Create Book
Get All Books after creation
Get Specific Book
Update Book
Delete Book
Get All Books after delete

So as we can see, all endpoints are working as they should, using postman makes it easy to test your own Web API’s.

Final considerations

Kotlin is an extremely powerful language, it allows you to develop much faster than when using Java. It also allows you to develop mobile applications and also server-side applications.

What this post tries to achieve is to show a little of what you can do when using Kotlin Spring to develop your server-side application.

I hope you enjoyed :)

--

--