Search code examples
serializationkotlinktorkotlin-exposed

Inserting data into database returns MismatchedInputException error


I am trying to insert some data into the database, and am getting the following error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.joda.time.DateTime` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('2019-04-19')

My content negotiation

install(ContentNegotiation) {
        jackson {
            enable(SerializationFeature.INDENT_OUTPUT)
        }
    }

And my model:

data class User(
//some codes
val registrationDate = DateTime  // org.joda.time.DateTime
)

And when will I send by json:

{
 //some other data
 "registrationDate" : "2019-07-15"
}

Can someone help me, please?


Solution

  • You have to install the Joda module for Jackson https://github.com/FasterXML/jackson-datatype-joda and add it to your jackson configuration in ktor :

    install(ContentNegotiation) {
            jackson {
                registerModule(JodaModule())
                enable(SerializationFeature.INDENT_OUTPUT)
            }
        }
    

    You can also control serialization/deserialization behavior with annotations on your data class properties :

    data class Account(
        val uid: String? = null,
        val firstName: String,
        val lastName: String,
        val email: String,
        @get:JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
        val createdTime: DateTime? = null
    )