我试图在LinkedHashMap
测试用例中映射JSON
中的kotlin
。当我使用json
将object
转换为jackson-objectmapper
时,出现以下问题,请帮助我解决此问题。
construct instance of java.util.LinkedHashMap (although at least one
Creator exists): no String-argument constructor/factory method to
deserialize from String value ('{e123f1dd-6c54-4b01-90e6-
d701748f0851=class MyClass {
下面的我的Kotlin
测试用例
fun `testcase success`() {
val mapId = UUID.fromString("e123f1dd-6c54-4b01-90e6-d701748f0851");
val Idno = UUID.randomUUID();
val order = MyClass();
val map : LinkedHashMap<String, MyClass> = LinkedHashMap();
order.collectiveSignature=true
order.docId="5436576"
order.status=OnboardOrderPoaStatusTPL.NOT_SIGNED
order.createdAt=createdDate
order.updatedAt=updatedDate
order.portingAt=OffsetDateTime.parse("2018-09-21T14:22:24.682+02:00");
order.fastPorting=true
order.owner=MyAnotherClass();
order.owner.company="test"
order.owner.firstName="test"
order.owner.lastName="test"
order.owner.city="test"
order.owner.zip="5436"
order.owner.street="test"
order.owner.house="test"
map["e123f1dd-6c54-4b01-90e6-d701748f0851"] = order;
val body = """{
"mapId": "$mapId",
"Idno": "$Idno",
"name": "Abishek",
"poas":"$map"
}""".trimIndent()
val mapper = ObjectMapper()
mapper.findAndRegisterModules()
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, false);
val input = mapper.readValue<MainClass>(body)
}
答案 0 :(得分:1)
问题是poas
的值包含Map
的字符串表示形式,其中包含MyClass
的字符串表示形式。
确实,创建Json
表示形式(您称为MyClass.toString()
)的目的和目的toString
的用途是显示调试信息。
如何解决?
使用ObjectMapper
应该将Map
转换为json,而不要使用toString()
val body = """{
"mapId": "$mapId",
"Idno": "$Idno",
"name": "Abishek",
"poas": "${mapper.writeValue(map)}"
}""".trimIndent()