Hello everyone! I’m having an issue with Ktor de-s...
# ktor
d
Hello everyone! I’m having an issue with Ktor de-serializing json in a post request. I’m using
kotlinx.serialization
and the exception occurs with
call.receive<MyMode>
. I’m curious if it a similar issue is the same as @nrobi. I don’t get the
java.lang.NoClassDefFoundError
but I do get
io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to
. I followed the instructions in https://ktor.io/servers/features/content-negotiation/serialization-converter.html and https://ktor.io/servers/calls/requests.html. I have the correct grade entry in my build.gradle file:
Copy code
implementation "io.ktor:ktor-serialization:$ktor_version"
I install the ContentNegotiator:
Copy code
fun Application.module() {
    install(ContentNegotiation) {
        serialization(
            contentType = ContentType.Application.Json,
            json = Json(DefaultJsonConfiguration.copy(prettyPrint = true))
        )
    }

    val dao = DAOFacadeDatabase(Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver"))
    val server = embeddedServer(Netty, port = 8080) {
I have my simple data class:
Copy code
data class UrlMap(val id: String, val url: String, val userId: String)
The exception is thrown here:
Copy code
val receive = call.receive<UrlMap>()
I follow multiple examples on website and the official Ktor documentation and I can’t get it to work. I’m using Ktor 1.3.0 with Kotlin 1.3.61. Any help would be greatly appreciated
👍 1
t
Cannot transform this request
It sounds like your request data doesn’t match what the content negotiator expects
Maybe the request body doesn’t contain the json data you’re expecting?
d
@Tim Malseed I double checked the payload and nothing obvious stands out. My payload:
Copy code
{
  "id": "7",
  "url": "<https://kotlinlang.org/>",
  "userId": "user1"
}
Data Class:
Copy code
data class UrlMap(val id: String, val url: String, val userId: String)
Also I doublechecked the headers as well, making sure this was present:
Copy code
Content-Type: application/json
n
@jetbrains could someone look into this please? @Artyom Degtyarev [JB] @e5l 🙏🏼
e
@sandwwraith
l
Isn't the data class missing the
@Serializable
annotation?
2
d
@louiscad - I went through several examples on various blogs and from the official Ktor website. I didn't see any data class with
@Serializable
.
n
@David Gethers could you try out with the annotations? Let’s see if it helps, I’m using the annotations but still having the problem
d
@nrobi I added the annotation and I'm still getting the same error. I did notice that IntelliJ is displaying the following warning:
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
Let me see how I can get rid of this warning.
@nrobi The interesting thing is that I tried several different Content Negotiations and I get the same error.
n
try adding
Copy code
id("kotlinx-serialization")
plugin, let’s see if it helps
d
@nrobi I added the plugin and I'm still getting the same error
l
Could any of you set up a reproducing project and give the GitHub/Gitlab/Bitbucket/Whatever link?
d
@louiscad - the changes are on a branch called:
adding-url-mapping
l
@David Gethers Hopefully, this information can help @sandwwraith or one of his colleagues help you 🙂 I initially had a few ideas, but I don't really have experience with ktor yet, I'm only getting started, so JB folks or other users can probably help further with the sample on the branch you specified.
👍 1
s
It looks fine from the serialization point of view. @e5l maybe ktor misconfiguration?
👍 1
n
@sandwwraith on my end probably yes, since it works well on the ktor-client side, but fails on the server side
e
@cy
d
@cy - any thoughts?
c
@David Gethers i'm having my lazy day today and managed to fix it for you 🙇🏻 so what happened here was that ktor was started up twice: once through Netty's
EngineMain
and then the second time in the
Application.module()
definition through calling
embeddedServer()
. after solving that, there was a simple
transaction {}
missing in your DAO. my patch attached here also contains the proper test (test first 🙌🏻 ). hope that helps, all the best and enjoy ktor, christoph
🎉 1
d
Thank you @christoph.pickl