diff --git a/kotlin-ktor/.gitignore b/kotlin-ktor/.gitignore index 1db5e66882..0c017e8f8c 100644 --- a/kotlin-ktor/.gitignore +++ b/kotlin-ktor/.gitignore @@ -7,6 +7,7 @@ #ignore build and generated files build/ node/ +out/ #ignore installed node modules and package lock file node_modules/ diff --git a/kotlin-ktor/build.gradle b/kotlin-ktor/build.gradle index 95c993b923..11aef74857 100755 --- a/kotlin-ktor/build.gradle +++ b/kotlin-ktor/build.gradle @@ -37,7 +37,7 @@ repositories { dependencies { compile "io.ktor:ktor-server-netty:$ktor_version" compile "ch.qos.logback:logback-classic:1.2.1" - compile "com.google.code.gson:gson:2.8.1" + compile "io.ktor:ktor-gson:$ktor_version" testCompile group: 'junit', name: 'junit', version: '4.12' } diff --git a/kotlin-ktor/out/production/classes/META-INF/KtorExample_main.kotlin_module b/kotlin-ktor/out/production/classes/META-INF/KtorExample_main.kotlin_module deleted file mode 100644 index 3a457da8fc..0000000000 Binary files a/kotlin-ktor/out/production/classes/META-INF/KtorExample_main.kotlin_module and /dev/null differ diff --git a/kotlin-ktor/resources/application.conf b/kotlin-ktor/resources/application.conf deleted file mode 100755 index 111af5304c..0000000000 --- a/kotlin-ktor/resources/application.conf +++ /dev/null @@ -1,5 +0,0 @@ -ktor { - application { - modules = [ io.ktor.samples.HelloKt.main ] - } -} \ No newline at end of file diff --git a/kotlin-ktor/src/main/kotlin/APIServer.kt b/kotlin-ktor/src/main/kotlin/APIServer.kt index 3aa1fa96f9..e67609e8b2 100755 --- a/kotlin-ktor/src/main/kotlin/APIServer.kt +++ b/kotlin-ktor/src/main/kotlin/APIServer.kt @@ -1,12 +1,16 @@ @file:JvmName("APIServer") -import com.google.gson.Gson + + import io.ktor.application.call import io.ktor.application.install import io.ktor.features.CallLogging +import io.ktor.features.ContentNegotiation import io.ktor.features.DefaultHeaders +import io.ktor.gson.gson import io.ktor.http.ContentType import io.ktor.request.path +import io.ktor.response.respond import io.ktor.response.respondText import io.ktor.routing.get import io.ktor.routing.routing @@ -14,6 +18,7 @@ import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty import org.slf4j.event.Level +data class Author(val name: String, val website: String) fun main(args: Array) { val jsonResponse = """{ @@ -22,7 +27,6 @@ fun main(args: Array) { "description": "Pay water bill today", }""" - data class Author(val name: String, val website: String) embeddedServer(Netty, 8080) { install(DefaultHeaders) { @@ -33,15 +37,18 @@ fun main(args: Array) { filter { call -> call.request.path().startsWith("/todo") } filter { call -> call.request.path().startsWith("/author") } } + install(ContentNegotiation) { + gson { + setPrettyPrinting() + } + } routing { get("/todo") { call.respondText(jsonResponse, ContentType.Application.Json) } get("/author") { val author = Author("baeldung", "baeldung.com") - val gson = Gson() - val json = gson.toJson(author) - call.respondText(json, ContentType.Application.Json) + call.respond(author) }