0

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?

4

1 回答 1

2

您必须为 Jackson https://github.com/FasterXML/jackson-datatype-joda安装 Joda 模块,并将其添加到您在 ktor 中的 jackson 配置中:

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

您还可以使用数据类属性上的注释来控制序列化/反序列化行为:

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
)

于 2019-07-19T02:02:58.237 回答