jackson Intellij IDEA在启用与未启用KotlinREPL的情况下返回不同的结果

ogsagwnx  于 2022-11-08  发布在  IntelliJ IDEA
关注(0)|答案(1)|浏览(121)

我在Intellij IDEA中创建了一个Kotlin临时文件,并使用我当前项目的模块类路径来访问项目的所有库(在本例中我使用Jackson)
在这两种情况下,我都声明了以下类:

class Test(var first: String = "a", var second: String = "b")

未启用REPL

val jsonAsString = "{\"first\": \"a\", \"second\":\"b\"}"

println(ObjectMapper().readValue(jsonAsString, Test::class.java).first) // prints out "a"

按预期打印出“a”
在启用REPL的情况下,ObjectMapper.readValue()会抛出以下异常

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `Line_2$Test` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"first": "a", "second":"b"}"; line: 1, column: 2]
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1904)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:351)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:184)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3597)

相关问题