์ง๋ ํฌ์คํ
https://rkdrkd-history.tistory.com/52
[Kotlin, Ktor] ktor ํ๋ก์ ํธ ์์ํ๊ธฐ(1)
์น๊ตฌ๋ค์ด๋ ํจ๊ป ํ๋ ํ๋ก์ ํธ์์ ์๋ฒ ๋ถ๋ถ์ ktor๋ก ๋ฐ๊ธฐ๋ก ๊ฒฐ์ ๋ฌ๋ค. ์ฒ์ ๋ค์ด๋ณธ ktor์ด์ง๋ง, ๊ฒ์์ ์ข ํด๋ณด๋๊น kotlin์ผ๋ก ๋ง๋ค ์ ์๋ ์๋ฒ๋ผ๋๋ผ ์ด๋ฒ ๊ธฐํ์ ๊ณต๋ถํด ๋ณด๋ฉด์ ktor์ ๋ํด
rkdrkd-history.tistory.com
์ง๋ ํฌ์คํ ์์ ktor project๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์๋ค.
์ด๋ฒ ํฌ์คํ ์์๋ ktor๋ก HTTP api๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ๋ค.
ktor ์๋ฒ ์คํํ๊ธฐ
์ผ๋จ ktor์๋ฒ๋ฅผ ์คํ์์ผ ๋ณด์.
ktor project๋ฅผ ๋ง๋ค๋ฉด ๊ธฐ๋ณธ์ผ๋ก ๋์ค๋ ์ํ ์ฝ๋๊ฐ ์๋ค(์ฒ์์ ๋ง๋ค ๋ ์ค์ ์ผ๋ก ์ํ ์ฝ๋ ์์ฑ์ ์ ํ๊ฒ ํ ์๋ ์๋ค).
Routing.kt
package com.example.plugins
import io.ktor.server.routing.*
import io.ktor.server.response.*
import io.ktor.server.application.*
fun Application.configureRouting() {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}
์ด ์ฝ๋๋ฅผ ๊ฐ์ง๊ณ ์๋ฒ๋ฅผ ์คํํด ๋ณด๊ฒ ๋ค.
Application.kt๋ก ์ด๋ํ ํmain ํจ์์์ ์คํํด ๋ณด๋๋ก ํ์.
์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ด๋ ๊ฒ ๋์ค๋ฉด ์ฑ๊ณต์ ์ผ๋ก ์๋ฒ ์คํ์ ๋ง์น ๊ฒ์ด๋ค.
๊ฐ๋จ api ๋ง๋ค๊ธฐ
๊ณต์ ๋ฌธ์์ ๋์ ์๋ ์ฝ๋๋ฅผ ์ด์ฉํด ๊ฐ๋จํ api๋ฅผ ์์ฑํด ๋ณด๋๋ก ํ๊ฒ ๋ค.
Customers.kt
package com.example.models
import kotlinx.serialization.Serializable
@Serializable
data class Customers(
val id: String,
val firstName: String,
val lastName: String,
val email: String
)
val customerStorage = mutableListOf<Customers>()
ktor์์ data ํด๋์ค๋ฅผ ๋ง๋ค์ด ์ค๋ค.
@Serializable ์ด๋ ธํ ์ด์ ์ด ktor ์๋ฒ์ ์๋ต json์ ์์ฑํด ์ค๋ค.
1. GET
get {
if (customerStorage.isNotEmpty()) call.respond(customerStorage)
else call.respondText("No Customer Found.", status = HttpStatusCode.OK)
}
get("{id}") {
val id = call.parameters["id"] ?: return@get call.respondText(
text = "Missing Id",
status = HttpStatusCode.BadRequest
)
val customer = customerStorage.find { it.id == id } ?: return@get call.respondText(
text = "No Customer with id $id",
status = HttpStatusCode.NotFound
)
call.respond(customer)
}
์ฒซ ๋ฒ์งธ get์ customer๋ค์ ์ ๋ณด๋ฅผ ๋์ดํ๋ api์ด๊ณ
๋ ๋ฒ์งธ get์ customerStorage์ id๋ฅผ ๊ธฐ์ค์ผ๋ก ํ ๋ช ์ customer๋ง ์กฐํํ๋ ๋ถ๋ถ์ด๋ค.
- id๋ฅผ ๋จผ์ ๋ถ๋ฌ์จ๋ค.
id๊ฐ null์ด๋ฉด missing id text๋ฅผ ์ถ๋ ฅํ๋๋ก ํ๋ค. - customer ์ ๋ณด๋ฅผ ๋ถ๋ฌ์จ๋ค.
find๋ฅผ ์ด์ฉํด ๊ฐ์ id์ ์ ๋ณด๋ง ๋ฝ์์จ๋ค.
์์ผ๋ฉด not found ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
2. POST
post {
val customer = call.receive<Customers>()
customerStorage.add(customer)
call.respondText(
text = "Customer stored correctly",
status = HttpStatusCode.Created
)
}
post๋ฅผ ์ด์ฉํด customer ์ ๋ณด๋ฅผ ์ ์ฅํ๋ค.
3. DELETE
delete("{id}") {
val id = call.parameters["id"] ?: return@delete call.respond(HttpStatusCode.BadRequest)
if (customerStorage.removeIf { it.id == id })
call.respondText(
text = "Customer remove correctly",
status = HttpStatusCode.Accepted
)
else call.respondText(text = "Not found.", status = HttpStatusCode.NotFound)
}
delete๋ก ํน์ id์ customer๋ฅผ ์ญ์ ํ๋ค.
4. Response๋ฅผ JSON์ผ๋ก ์ค์
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
์ด ์ฝ๋๋ ktor ์๋ฒ์ response๊ฐ์ json์ผ๋ก ์ค์ ํด ์ฃผ๋ ๋ถ๋ถ์ด๋ค.
์ด ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์ถํ์ android studio์์ retrofit์ผ๋ก ํต์ ์ ํ ๋ 415 ์๋ฌ(Unsupported Media Type)๊ฐ ๋ฐ์ํ ์ ์์ผ๋ ์์ฑํ๋๋ก ํ์(์ค์ ๋ก ๊ฒช์์๋ค...ใ ).
ktor์์ post ๋ถ๋ถ์ receive <T> ๋ถ๋ถ์ T๊ฐ์ฒด๋ฅผ request body๋ก ๋ฐ๋ ๊ฒ์ ๋ปํ๊ณ
respond๋ ํน์ ํ์ ๊ฐ์ฒด๋ฅผ api response๋ก ๋ฐํํ๋ ๊ฒ์ ๋ปํ๋ค.
- call.receive <Customers>: Customers ๊ฐ์ฒด๋ฅผ request body๋ก ๋ฐ๊ธฐ
- call.respond(Customers(... )): Customers ๊ฐ์ฒด๋ฅผ api response๋ก ์ง์
์ ์ฒด ์ฝ๋
CustomerRoutes.kt
package com.example.routes
import com.example.models.Customers
import com.example.models.ResponseData
import com.example.models.customerStorage
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
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 kotlinx.serialization.json.Json
fun Route.customerRouting() {
route("/customer") {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
get {
if (customerStorage.isNotEmpty()) call.respond(customerStorage)
else call.respondText("No Customer Found.", status = HttpStatusCode.OK)
}
get("{id}") {
val id = call.parameters["id"] ?: return@get call.respondText(
text = "Missing Id",
status = HttpStatusCode.BadRequest
)
val customer = customerStorage.find { it.id == id } ?: return@get call.respondText(
text = "No Customer with id $id",
status = HttpStatusCode.NotFound
)
call.respond(customer)
}
post {
val customer = call.receive<Customers>()
customerStorage.add(customer)
call.respond(
status = HttpStatusCode.Created,
message = ResponseData("Customer stored correctly")
)
}
delete("{id}") {
val id = call.parameters["id"] ?: return@delete call.respond(HttpStatusCode.BadRequest)
if (customerStorage.removeIf { it.id == id })
call.respondText(
text = "Customer remove correctly",
status = HttpStatusCode.Accepted
)
else call.respondText(text = "Not found.", status = HttpStatusCode.NotFound)
}
}
}
์ผ๋จ ๊ฐ๋จํ๊ฒ get, post, delete ๋ก์ง์ ๋ง๋ค์ด ์ฃผ์๋ค.
ํ์ผ ๊ตฌ์กฐ(ktor)
Android์์ Retrofit์ผ๋ก ์๋ฒ ํ ์คํธํ๋ ์ฝ๋๋ ์๋ตํ๊ฒ ๋ค.
์ ๋ฆฌ
ktor๋ก http api๋ฅผ ๋ง๋ค์ด๋ดค๋๋ฐ ๋ง๋ค๊ณ ๋์ android์ ์ฐ๋ํด ํ ์คํธํ ๋ response ํ์ ์ ๋ง์ถ์ง ์์์ 415 ์๋ฌ๊ฐ ๊ณ์ ๋ฐ์ํ์๋๋ฐ ๊ฐ๋จํ ๋ฌธ์ ์๋๋ฐ ํ๋ฃจ๋ฅผ ๋ ๋ ธ์๋ค...ใ
๋ค์ ํฌ์คํ ์์๋ ktor์์ database๋ฅผ ์ด์ฉํด ๋ณด๋๋ก ํ๊ฒ ๋ค.
'๐ | ๊ธฐํ ๊ธฐ์ > ๐ฅ๏ธ | Ktor' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin, Ktor] ktor๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๋ํ๊ธฐ - 1(3) (0) | 2023.06.15 |
---|---|
[Kotlin, Ktor] ktor ํ๋ก์ ํธ ์์ํ๊ธฐ(1) (4) | 2023.06.12 |