Kotlin has been utilised for server-side development for a long time – with several frameworks and libraries. Some of the libraries have good Kotlin support, and some don’t. That is quickly changing as more and more libraries are popping up that are Kotlin-native. A few of the libraries discussed below are not newly created, but they are garnering good traction in the last few months and have good support to develop server-side applications.
1. Kotless
Of the technologies that are relevant for 2021, serverless is one of them. Kotless is just for that. It stands for Kotlin serverless framework. It focuses on reducing serverless deployment creation by generating it from the code directly. It simply gives you an easy way to deploy your serverless application on AWS! Kotless uses Gradle to wrap around the existing build process and insert the deployment into it. It will act as a wrapper for your application. Kotless is able to deploy existing Spring Boot or Ktor applications to AWS serverless platforms.
2. Kotest
Kotest is a testing library. In any testing library, there are three components:
- The testing framework
- The mocking framework
- The assertion framework.
You can generally mix and match like JUnit, Mockk, and assertj. Kotest provides you with the testing framework and the assertion framework.
So a typical test case would look something like this with Kotest and Mockk.
|
Kotest has the advantage of being written in Kotlin. So it can leverage Kotlin’s language features, which allows you to write more idiomatic and concise assertions. It has built-in coroutine support, the ability to use functions as test lifecycle callbacks, advanced conditional evaluation, powerful data-driven testing, and more.
3. Exposed
Exposed is a lightweight SQL library that can be used with a Kotlin web project. It is very easy to set up and use. It follows the important design rule that relies heavily on simplicity and minimalism. So if you just want a row update, it is pretty straightforward. The library provides you with all the constructs to take control of almost everything from transactions to isolations. It supports DSL syntax, which makes it more flexible and easy to reason the SQL code.
It has a good “getting started” guide, and the documentation is getting better. Sometimes you have to dig deeper to find the solution to a particular problem, like transaction isolation or thread pool, which btw makes it interesting to use and work with as well.
4. Ktor
Ktor is a framework for building asynchronous servers and clients in connected systems using Kotlin. It’s concise and clear. And that is exactly what the framework makes you feel when you use it. It is minimal and easy to set up and use. It has a lot less complexity, but sometimes you need to build more things yourself, which is true with any library that is minimal and simple. It's like having a small toolbox that works extremely well.
Spring, on the other hand, is like having the whole hardware store. Everything is in there somewhere, but there is a lot more navigating; it’s bulky and magical.
With that said, if you need a small application without any IoC or something, Ktor can solve most of your tasks. However, if you need a huge one with potential integration with Kafka, multiple databases, IoC in tests, etc., then you should select Spring.
5. Kotlinx Serialization
When you think of a JSON parser, only these names come to mind, Jackson, Gson, or Moshi. And those are the most widely used parsers with Spring and Java applications as well. Kotlin serialization is a Kotlin-oriented, cross-platform, reflectionless serialization library from JetBrains.
@Serializable |
And this is how you will read a JSON using this data class.
class JsonUnitTest { |
6. Koin
Koin is a DI framework for Kotlin developers, completely written in Kotlin. It comes in very handy when you are creating a Kotlin application using the libraries mentioned above. Say you are using Ktor, exposed for a Kotlin web application. You would need a DI framework to manage your controllers, services, repositories, etc.
This is where you would need Koin. While working with Koin, there are few terminologies we need to understand before getting started.
- module — creates a module in Koin which would be used by Koin to provide all the dependencies
- single — creates a singleton that can be used across the app as a singular instance
- factory — provides a bean definition, which will create a new instance each time it is injected
- get() — is used in the constructor of a class to provide the required dependency.
|
7. Netflix DGS framework
Netflix recently released a library called Netflix DGS to create GraphQL servers in Kotlin and Java. It is open-sourced and is production-ready as Netflix has already been using this internally for quite some time now. DGS framework ties really well with Spring boot and is built on top of graphql-java.
This library helps you create a project just as easily as creating a Spring Boot project.
8. KMongo
KMongo is a library that does for MongoDB what Exposed is doing for the SQL databases. It supports both synchronous and asynchronous patterns, and you can use coroutines with it. It is a fairly new library and is not used so much yet. This can be used with Kotlin serialization as well for object mapping. This is what makes it a good choice if your Kotlin application interacts with MongoDB and you are not using Spring Boot.
If you don’t use KMongo then you will have to use the Java driver for MongoDB. The below example utilizes KMongo and Kotlin serialization to create an entity, save it, and retrieve it. It is really easy to use and simple to set up.
|
9. Xodus
Another one in the database category is Xodus. Xodus is a transactional, schema-less, embedded database that is written in Java and Kotlin. A few good things about Xodus are:
- ACID on-board — All database operations are atomic, consistent, isolated, and durable.
- No need to manage an external database — Everything is inside your application.
- Kotlin language benefits — It takes the best from using types, nullable values, and delegates for properties declaration and constraints description.
- Painless refactorings — If you need to add a couple of properties, you won’t have to then rebuild the tables.
10. Dokka
A Javadoc replacement for Kotlin. It is a documentation engine made for Kotlin and has around 2k stars on GitHub. It has loads of configuration options and supports Kotlin multiplatform. The language used to document Kotlin code is called KDoc. Dokka understands standard Javadoc comments in Java files and KDoc comments in Kotlin files and can generate documentation in multiple formats including minimalistic HTML, Javadoc HTML, and Markdown.
11. Vaadin
Another web framework with support for Kotlin using Kotlin DSL. With the Kotlin DSL for Vaadin, you can start adding your content to the UI class straight away. With less boilerplate, you can focus only on building your app. Vaadin lets you forget about the web and develop user interfaces in much the same way as you would a desktop application with conventional Java toolkits like Swing. Kotlin is a great fit for developing web applications because it allows you to write concise and expressive code.