I'm trying to create a simple server that parses json and makes some logs.
package com.example
import com.example.plugins.*
import io.ktor.http.content.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.slf4j.LoggerFactory
fun main(args: Array<String>) {
io.ktor.server.netty.EngineMain.main(args)
}
fun Application.module() {
configureSerialization()
configureDatabases()
configureMonitoring()
configureHTTP()
configureSecurity()
configureRouting()
}
data class LoginData(val username: String, val password: String)
fun Application.configureSerialization() {
install(ContentNegotiation) { json() }
}
fun Application.configureRouting() {
val logger = LoggerFactory.getLogger("Application")
routing {
post("/login") {
logger.info("GET SOME RESPONSE")
val loginData = call.receive<LoginData>()
val username = loginData.username
val password = loginData.password
logger.info("GET SOME RESPONSE: $username")
println("GET SOME RESPONSE: $username")
if (username == "admin" && password == "admin123") {
logger.info("Authorization successful for user: $username")
call.respondText("Authorization successful!")
} else {
logger.info("Authorization failed for user: $username")
call.respondText("Authorization failed!")
}
}
}
}
build.gradle.kts:
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
plugins {
kotlin("jvm") version "1.9.24"
id("io.ktor.plugin") version "2.3.11"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.24"
}
group = "com.example"
version = "0.0.1"
application {
mainClass.set("io.ktor.server.netty.EngineMain")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories { mavenCentral() }
dependencies {
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("org.postgresql:postgresql:42.5.1")
implementation("com.h2database:h2:2.1.214")
implementation("io.ktor:ktor-server-metrics-jvm")
implementation("io.ktor:ktor-server-caching-headers-jvm")
implementation("io.ktor:ktor-server-auth-jvm")
implementation("io.ktor:ktor-server-netty-jvm")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-config-yaml:2.3.11")
testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
I get a simple error: "Unresolved reference: json"
It looks like I'm importing something wrong and I read the documentation, but it didn't help. Since I’m a newbie, I’m not entirely sure that I understand what plugins are for and how they differ from dependencies; from my experience with java, I haven’t encountered this, I would appreciate any help.
json
is an extension function declared in the io.ktor.serialization.kotlinx.json
package. You did not import that. You should add this import:
import io.ktor.serialization.kotlinx.json.json
// or
// import io.ktor.serialization.kotlinx.json.*
Also, LoginData
should be marked @Serializable
since you are doing call.receive<LoginData>()
.