I’m trying to learn how to use Ktor, but I ran into a problem. When I try to do this in main
:
val server = embeddedServer(Netty, port = 8080) {
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}
}
setPrettyPrinting()
cannot be found. I have the io.ktor:ktor-gson
dependency installed. What’s going on?
build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.4.21"
id("org.jetbrains.kotlin.plugin.serialization") version "1.4.21"
id("application")
}
val kotlinVersion: String by extra
val ktorVersion: String by extra
val logbackVersion: String by extra
val kotlinSerialization: String by extra
val exposedVersion: String by extra
val h2Version: String by extra
group = "com.theonlytails.learnktor"
version = "1.0-SNAPSHOT"
repositories {
jcenter()
mavenCentral()
maven(url = "https://kotlin.bintray.com/kotlinx")
}
dependencies {
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("ch.qos.logback:logback-classic:$logbackVersion")
implementation("io.ktor:ktor-serialization:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinSerialization")
implementation("io.ktor:ktor-client-apache:$ktorVersion")
implementation("io.ktor:ktor-gson:$ktorVersion")
implementation("org.jetbrains.exposed:exposed:$exposedVersion")
implementation("com.h2database:h2:$h2Version")
testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClassName = "com.theonlytails.learnktor.ApplicationKt"
}
Application.kt:
package com.theonlytails.learnktor
import com.theonlytails.learnktor.routes.registerCustomerRoutes
import com.theonlytails.learnktor.routes.registerOrderRoutes
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.gson.gson
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.EngineMain
import io.ktor.server.netty.Netty
import java.security.SecureRandom
fun main(args: Array<String>) {
EngineMain.main(args)
val secureRandom = SecureRandom()
val booleanRandomizer = { secureRandom.nextBoolean() }
val server = embeddedServer(Netty, port = 8080) {
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}
registerCustomerRoutes()
registerOrderRoutes()
}
}
Note: I’m following this tutorial.Preformatted text