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

David Gethers

02/04/2020, 11:56 PM
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

Tim Malseed

02/04/2020, 11:59 PM
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

David Gethers

02/05/2020, 3:08 AM
@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

nrobi

02/06/2020, 7:27 AM
@jetbrains could someone look into this please? @Artyom Degtyarev [JB] @e5l 🙏🏼
e

e5l

02/06/2020, 7:29 AM
@sandwwraith
l

louiscad

02/06/2020, 9:44 AM
Isn't the data class missing the
@Serializable
annotation?
2
d

David Gethers

02/07/2020, 3:11 PM
@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

nrobi

02/07/2020, 3:12 PM
@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

David Gethers

02/07/2020, 3:20 PM
@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

nrobi

02/07/2020, 3:22 PM
try adding
Copy code
id("kotlinx-serialization")
plugin, let’s see if it helps
d

David Gethers

02/07/2020, 3:36 PM
@nrobi I added the plugin and I'm still getting the same error
l

louiscad

02/07/2020, 4:43 PM
Could any of you set up a reproducing project and give the GitHub/Gitlab/Bitbucket/Whatever link?
d

David Gethers

02/07/2020, 7:19 PM
@louiscad - the changes are on a branch called:
adding-url-mapping
l

louiscad

02/08/2020, 5:13 PM
@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

sandwwraith

02/10/2020, 9:36 AM
It looks fine from the serialization point of view. @e5l maybe ktor misconfiguration?
👍 1
n

nrobi

02/10/2020, 9:38 AM
@sandwwraith on my end probably yes, since it works well on the ktor-client side, but fails on the server side
e

e5l

02/10/2020, 10:20 AM
@cy
d

David Gethers

02/12/2020, 8:55 PM
@cy - any thoughts?
c

christoph.pickl

02/16/2020, 11:28 AM
@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

David Gethers

02/18/2020, 4:49 PM
Thank you @christoph.pickl
11 Views