From 423ccc6a83e5de0529f9854c3efc422cc75c286c Mon Sep 17 00:00:00 2001 From: Syed Mansoor Date: Mon, 18 Jun 2018 23:31:04 +1000 Subject: [PATCH] [BAEL-1753] Cleanup code --- kotlin-ktor/.gitignore | 1 + kotlin-ktor/build.gradle | 2 +- .../META-INF/KtorExample_main.kotlin_module | Bin 31 -> 0 bytes kotlin-ktor/resources/application.conf | 5 ----- kotlin-ktor/src/main/kotlin/APIServer.kt | 17 ++++++++++++----- 5 files changed, 14 insertions(+), 11 deletions(-) delete mode 100644 kotlin-ktor/out/production/classes/META-INF/KtorExample_main.kotlin_module delete mode 100755 kotlin-ktor/resources/application.conf 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 3a457da8fc1f80c3d62dad3e91da31eaeadc7597..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 31 gcmZQzU|?ooU|@t|E-qd!1|d$z0MFpmqO#N?024+7od5s; 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) }