MismatchedInputException:无法构造`java.util.LinkedHashMap`的实例

时间:2019-05-30 06:02:32

标签: json kotlin jackson testcase

我试图在LinkedHashMap测试用例中映射JSON中的kotlin。当我使用jsonobject转换为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)


}

1 个答案:

答案 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()