Hi people, kinda new here... was wondering how can...
# ktor
j
Hi people, kinda new here... was wondering how can I accept a JSON payload in Ktor tried
call.receiveText()
but it gets is to a string - had a look on the docs about serialization, but got quite confused there.
b
call.receive<MyType>()
e.g. call.receive<JsonObject>()
m
that’s the wrong function, it just reads the request as text. First you have to install content negotiation feature:
Copy code
import io.ktor.features.*
// ...
fun main() {
    embeddedServer(Netty, port = 8080) {
        install(ContentNegotiation)
        // ...
    }.start(wait = true)
}
then register a converter, for example Jackson https://ktor.io/docs/jackson.html#register_jackson_converter. Then you execute what Martynas wrote above, yeah ⬆️
to receive data you ask for a given type of your choice and Ktor will perform deserialization for you https://ktor.io/docs/jackson.html#register_jackson_converter
a
Please read the docs about content negotiation and serialization
j
I have ContentNegotiation installed.. but when I call
call.receive<Entity>()
I get error that some values are not provided...
b
Does your json match data class? Is data class annotated as serializable?
j
The situation is that my Entity has computed values
Copy code
Entity:
id: Int,
user: User,
value: String,
computedValue: String
and Entity is a DAO for an EntityTable The problem is that calling
call.recieve<Entity>()
yells that I didn't provide
id, user, computedValue
in the payload
b
Annotate computed values with @JsonIgnore
j
Didnt know about @JsonIgnore.
Thanks!
Should also the id (auto incremented) should be annotiated?
b
Pretty much any serialization framework has something like that as it's a pretty standard problem 😄
j
First time using Exposed/Ktor 🙂 Coming from a Python/NodeJS world