我正在尝试将一些数据插入数据库中,并出现以下错误:
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')
我的内容协商
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
我的模特:
data class User(
//some codes
val registrationDate = DateTime // org.joda.time.DateTime
)
我什么时候通过json发送
{
//some other data
"registrationDate" : "2019-07-15"
}
有人可以帮我吗?
答案 0 :(得分:1)
您必须为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
)