jackson解析报错:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance

关注 安卓007 ,免费获取全套安卓开发学习资料

报错信息

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.cxyzy.note.server.response.LoginResp` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{
  "code" : 0,
  "message" : null,
  "data" : {
    "token" : "xxxxxx",
    "userId" : "xxxxxxx"
  }
}"; line: 5, column: 5] (through reference chain: com.cxyzy.note.server.response.BaseResp["data"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
	at com.cxyzy.note.server.test.UserTest$testLogin$2.invoke(UserTest.kt:54)
	at com.cxyzy.note.server.test.UserTest$testLogin$2.invoke(UserTest.kt:21)
	at io.ktor.server.testing.TestEngineKt$withTestApplication$1.invoke(TestEngine.kt:65)
	at io.ktor.server.testing.TestEngineKt$withTestApplication$1.invoke(TestEngine.kt)
	at io.ktor.server.testing.TestEngineKt.withApplication(TestEngine.kt:46)
	at io.ktor.server.testing.TestEngineKt.withApplication$default(TestEngine.kt:40)
	at io.ktor.server.testing.TestEngineKt.withTestApplication(TestEngine.kt:63)
	at com.cxyzy.note.server.test.UserTest.testLogin(UserTest.kt:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

LoginResp定义

class LoginResp(
    var token: String,
    var userId: String
)

解决方案

增加默认函数(无参数的)

class LoginResp(
    var token: String,
    var userId: String
) {
    /**
     * default constructor for jackson deserialization
     */
    constructor() : this(token = "", userId = "")
}

关注头条号,第一时间获取最新文章:

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页