1. ホーム
  2. ジャワ

jackson パースエラー:com.fastxml.jackson.databind.exc.InvalidDefinitionException: インスタンスを構築できません

2022-02-26 22:54:08
<パス

エラーメッセージ

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" : "xxxxxxx",
    "userId" : "xxxxxxxx"
  }
}"; 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)


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


LoginRespの定義

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


解決方法

デフォルト関数の追加(パラメータなし)

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


私について

厦門大学コンピューターサイエンス学科|元華為技術者
プログラミングのテクニックを共有し、深くはないが、理解しやすく、初心者に適している。
Java|Android|フロントエンド|スモールアプリ|ホンメン
公開します。 ピーナツスキンプログラミング