我正在使用 Jackson 将 ElasticSearch 文档解析为以下数据类
data class ElasticCity(
val id: Long,
val regionId: Long,
val countryIso: String,
val isFeatured: Boolean?
) {
// For now Jackson does not support this for constructor parameters https://github.com/FasterXML/jackson-databind/issues/562
@JsonAnySetter
val names: MutableMap<String, String> = mutableMapOf()
}
但是我收到以下错误(格式化我的)
com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException:
Instantiation of [simple type, class net.goout.locations.model.ElasticCity] value failed
for JSON property country_iso due to missing (therefore NULL) value for creator parameter countryIso which is a non-nullable type
at [Source: (byte[])
"{
"name.cs":"Brno",
"countryIso":"CZ",
"regionId":85682423,
"timezone":"Europe/Prague",
"name.de":"Brünn",
"name.sk":"Brno",
"id":101748109,
"isFeatured":true,
"name.pl":"Brno",
"name.en":"Brno"
}";
line: 1, column: 186] (through reference chain: net.goout.locations.model.ElasticCity["country_iso"])
显然是关键
countryIso
存在于 JSON 中,但由于某种原因 jackson 提示 key country_iso
不见了。为什么?我怎样才能解决这个问题?
最佳答案
尝试添加
data class ElasticCity(
val id: Long,
val regionId: Long,
@JsonProperty(value = "countryIso") val countryIso: String,
val isFeatured: Boolean?
)
jackson 映射器隐式转换非起始大写字符_
如果您想在多个地方解决此问题,请查看
@JsonNaming(PropertyNamingStrategy..
https://www.programcreek.com/java-api-examples/?api=com.fasterxml.jackson.databind.PropertyNamingStrategy
关于kotlin - 为什么 jackson 不能反序列化这个 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61461495/