Kotlin 数据 class 和 LocalDateTime
Kotlin data class and LocalDateTime
我有票 class:
data class Ticket(
var contact_email : String? = null,
var date_opened : LocalDateTime? = null
)
但我在读取字符串时出错:
Caused by:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of java.time.LocalDateTime
(no Creators, like
default construct, exist): no String-argument constructor/factory
method to deserialize from String value ('2017-11-13T06:40:00Z') at
[Source: UNKNOWN; line: -1, column: -1] (through reference chain:
rnd_classifier.model.Ticket["date_opened"])
我尝试添加注释但没有成功:
data class Ticket(
var contact_email : String? = null,
@JsonSerialize(using = ToStringSerializer::class)
@JsonDeserialize(using = LocalDateTimeDeserializer::class)
var date_opened : LocalDateTime? = null
)
如何解决?
您的问题更多是关于 jackson 而不是 kotlin。
如
所述
需要添加额外的gradle依赖才能解决
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.5")
之后它应该工作
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.testng.annotations.Test
import java.time.LocalDateTime
class SoTest {
data class Ticket(
var contact_email: String? = null,
var date_opened: LocalDateTime? = null
)
@Test
fun checkSerialize() {
val mapper = ObjectMapper()
mapper.registerModule(JavaTimeModule())
val ticket = mapper.readValue(inputJsonString, Ticket::class.java)
assert ("$ticket"=="Ticket(contact_email=contact@ema.il, date_opened=2017-11-13T06:40)")
}
val inputJsonString = """{
"contact_email": "contact@ema.il",
"date_opened": "2017-11-13T06:40:00Z"
}""".trimIndent()
}
我有票 class:
data class Ticket(
var contact_email : String? = null,
var date_opened : LocalDateTime? = null
)
但我在读取字符串时出错:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
java.time.LocalDateTime
(no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2017-11-13T06:40:00Z') at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: rnd_classifier.model.Ticket["date_opened"])
我尝试添加注释但没有成功:
data class Ticket(
var contact_email : String? = null,
@JsonSerialize(using = ToStringSerializer::class)
@JsonDeserialize(using = LocalDateTimeDeserializer::class)
var date_opened : LocalDateTime? = null
)
如何解决?
您的问题更多是关于 jackson 而不是 kotlin。
如
需要添加额外的gradle依赖才能解决
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.5")
之后它应该工作
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.testng.annotations.Test
import java.time.LocalDateTime
class SoTest {
data class Ticket(
var contact_email: String? = null,
var date_opened: LocalDateTime? = null
)
@Test
fun checkSerialize() {
val mapper = ObjectMapper()
mapper.registerModule(JavaTimeModule())
val ticket = mapper.readValue(inputJsonString, Ticket::class.java)
assert ("$ticket"=="Ticket(contact_email=contact@ema.il, date_opened=2017-11-13T06:40)")
}
val inputJsonString = """{
"contact_email": "contact@ema.il",
"date_opened": "2017-11-13T06:40:00Z"
}""".trimIndent()
}