Ktor Hello World Example

In the section, we will create a project for Hello Word Example.

Step 1: Open Ktor Initializr https://start.ktor.io/

Step 2: Provide the Project Name We have provided ktor-helloworld.

Step 3: Provide the Website name. We have provided the com.knf.dev.

Step 4: Provide the Artifact Id. We have provided the dev.knf.com.ktor-helloworld.

Step 5: Click on the Add plugins button.

Step 6: Click on the Generate project button. 



Step 7: Extract the ZIP file.

Step 8: Import the project folder

When the project imports successfully, it shows the following project directory in the Package Explorer section of the IDE.

Project Directory


Routing.kt

package dev.com.knf.plugins

import io.ktor.server.routing.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.request.*

fun Application.configureRouting() {

routing {
get("/") {
call.respondText("Hello World!")
}
}
}


Application.kt

package dev.com.knf

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import dev.com.knf.plugins.*

fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}

fun Application.module() {
configureRouting()
}

When the application runs successfully, it shows a massage in the console, as shown in the following figure.

Step 8: Open the browser and invoke the URL https://localhost:8080. It returns a String that we have specified in the Routing.kt.

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String