Skip to main content

Posts

Showing posts from April, 2021

How to create a Rest API in Kotlin using KTOR

If you came from a Java background you may be interested to know how you can build a rest api in kotlin. In this post we will create one using Ktor , but wait, what is ktor? According to their web site: Ktor is an asynchronous framework for creating microservices, web applications, and more. It’s fun, free, and open source. Just like any Java Spring Boot Application, Ktor also has its own Application file . So lets start from it by comparing a spring boot application file with a Ktor. A Ktor application file: As you can see, both files have a main method that receives as an argument an array of strings, but Ktor uses its main method to create an embedded netty application. In a Spring Application we would create a controller class responsible to handle all http requests to an endpoint by making use of java annotations. In Ktor, we need to create a kotlin file to represent our Application Routes by using the Ktor Route class . As you can see we make use of the route metho