Build Your First Rest API in Ktor
What is Ktor?
Ktor is a framework build in kotlin language which helps in building web
applications, http services etc. Most of the Services needs to fast and secure
these days,ktor helps us create asynchronous client and server applications in
simple way.
Why Ktor?
- Ktor is Lightweight and Flexible framework.
- Ktor allows us to use only what we need, and to structure our application the way we need it.
-
We can easily extend the capability of the framework by using plugins.
- As it is coroutine-native thus helps us build large scale server-side applications handling billions of events / day.
Lets Build Our First Rest API in Ktor
To build Rest API in ktor we would need to generate ktor project first, we can
do that using the ktor plugin in Ultimate edition of Intellij Idea.
We can also visit https://start.ktor.io and generate the project.
Project Generator you need to add your project name, select server engine and
other setting also select plugins we need. For our first API we will need
Routing plugin for handling requests and content negotiation to work with
Serialization and Deserialization of request Body.
Unzip the folder created and import the project in the IDE.
KTOR generator will add some sample code to begin with, for our purpose we
will remove all the sample code and write our own code in most easy way to get
started.
Later on in the subsequent posts we will properly structure our code base and
use best coding practices.
Project Structure :
The Initiation point of any app is the main method, here as well we have
a main method which will be used to configure our server and set
necessary configurations.
We have used HOCON file based configuration approach here.
Application.Kt File:
package kodesrc
import io.ktor.serialization.gson.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun main(args: Array <String>) {
io.ktor.server.netty.EngineMain.main(args)
}
fun Application.initializeApp() {
install(ContentNegotiation) {
gson {
}
}
configureRouting()
}
fun Application.configureRouting() {
routing {
get("/welcome-to-kodesrc") {
call.respondText("Welcome to KodeSrc!")
}
get("/hello-world") {
call.respondText("Hello World!")
}
}
}
Understanding the Application.Kt File
If we need to use and plugins we need to inform the application to install the
library other wise we will not be able to use that particular library
functionalities. We have used Gson Library for content negotiation.
The Application Class from io.ktor.server.application package is used and it
Represents configured and running web application, capable of handling
requests. It is also the application coroutine scope that is cancelled
immediately at application stop so useful for launching background coroutines.
The routing{} scope it installs a Routing plugin for the this Application and
runs a configuration script on it.
The get ("/welcome-to-kodesrc"){} this is the actual API scope that will get
executed when http://localhost:8080/welcome-to-kodesrc URL is hit in
browser.
application.conf File:
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ kodesrc.ApplicationKt.initializeApp ]
}
}
This is a HOCON file where all the configurations related to the Application
is maintained.
That is all you need to create your first Rest API in Ktor. Now run the main
method and access the URL in browser.
That is all for this Post. We have easily created our First Rest API in Ktor.
You can find the source code at github here :
KTOR Rest API - WelcomeToKodeSrc
Interested in understanding few more topics in Java?
Check it Out !
Feel Free to reach us out through the comment section or use contact page.
Please Let me Know, If you have any doubts.
Please Let me Know, If you have any doubts.