Skip to content

Commit 423ccc6

Browse files
committed
[BAEL-1753] Cleanup code
1 parent 86fe6cf commit 423ccc6

5 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎kotlin-ktor/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ignore build and generated files
88
build/
99
node/
10+
out/
1011

1112
#ignore installed node modules and package lock file
1213
node_modules/

‎kotlin-ktor/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ repositories {
3737
dependencies {
3838
compile "io.ktor:ktor-server-netty:$ktor_version"
3939
compile "ch.qos.logback:logback-classic:1.2.1"
40-
compile "com.google.code.gson:gson:2.8.1"
40+
compile "io.ktor:ktor-gson:$ktor_version"
4141
testCompile group: 'junit', name: 'junit', version: '4.12'
4242

4343
}
Binary file not shown.

‎kotlin-ktor/resources/application.conf‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎kotlin-ktor/src/main/kotlin/APIServer.kt‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
@file:JvmName("APIServer")
22

3-
import com.google.gson.Gson
3+
4+
45
import io.ktor.application.call
56
import io.ktor.application.install
67
import io.ktor.features.CallLogging
8+
import io.ktor.features.ContentNegotiation
79
import io.ktor.features.DefaultHeaders
10+
import io.ktor.gson.gson
811
import io.ktor.http.ContentType
912
import io.ktor.request.path
13+
import io.ktor.response.respond
1014
import io.ktor.response.respondText
1115
import io.ktor.routing.get
1216
import io.ktor.routing.routing
1317
import io.ktor.server.engine.embeddedServer
1418
import io.ktor.server.netty.Netty
1519
import org.slf4j.event.Level
1620

21+
data class Author(val name: String, val website: String)
1722
fun main(args: Array<String>) {
1823

1924
val jsonResponse = """{
@@ -22,7 +27,6 @@ fun main(args: Array<String>) {
2227
"description": "Pay water bill today",
2328
}"""
2429

25-
data class Author(val name: String, val website: String)
2630

2731
embeddedServer(Netty, 8080) {
2832
install(DefaultHeaders) {
@@ -33,15 +37,18 @@ fun main(args: Array<String>) {
3337
filter { call -> call.request.path().startsWith("/todo") }
3438
filter { call -> call.request.path().startsWith("/author") }
3539
}
40+
install(ContentNegotiation) {
41+
gson {
42+
setPrettyPrinting()
43+
}
44+
}
3645
routing {
3746
get("/todo") {
3847
call.respondText(jsonResponse, ContentType.Application.Json)
3948
}
4049
get("/author") {
4150
val author = Author("baeldung", "baeldung.com")
42-
val gson = Gson()
43-
val json = gson.toJson(author)
44-
call.respondText(json, ContentType.Application.Json)
51+
call.respond(author)
4552

4653
}
4754

0 commit comments

Comments
 (0)