https://kotlinlang.org logo
#ktor
Title
# ktor
e

Emtee

01/18/2023, 10:42 AM
hi. Can somebody please tell me what’re the benefits of using type-safe routing? The only one I can think of is behind-the-scene mapping of query and path params to the resource fields, so you can write
Copy code
resource.id
instead of
Copy code
call.parameters["id"]
c

CLOVIS

01/18/2023, 11:07 AM
You can write
Copy code
resource.someInt
instead of
Copy code
call.parameters["someInt"]?.run {
    call.respondText("Missing parameter 'someInt'", HttpStatusCode.UNPROCESSABLE_ENTITY)
    return@route
}.toIntOrNull()?.run {
    call.respondText("Parameter 'someInt' should be an integer", HttpStatusCode.UNPROCESSABLE_ENTITY)
    return@route
}
Also, you can use the same classes client-side, so refactoring a query parameter in IDEA changes it on the entire stack without having to manually search through all usages manually. It's a bit limited however because only the request is typesafe (query params + body) and not the response.
💡 2
e

Emtee

01/18/2023, 1:24 PM
Gotcha. Thank you
11 Views