将数据插入数据库 returns MismatchedInputException 错误

Inserting data into database returns MismatchedInputException 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')

我的内容协商

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"
}

有人可以帮帮我吗?

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

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

您还可以通过数据 class 属性的注释来控制 serialization/deserialization 行为:

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
)