gpt4 book ai didi

java - 找不到类型[简单类型,类定义]的合适构造函数

转载 作者:行者123 更新时间:2023-12-01 08:54:44 25 4
gpt4 key购买 nike

我使用 Jackson 框架编写了一个 Scala 程序来读取 Json 文件。每当执行 Scala 程序时,我都会收到以下错误。谁能建议我如何克服这个错误。

错误

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class Definition]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: { "recordDefinitions": [ { "recordDefinitionIdentifier": 2, "recordTypeCode": "LT", "recordTypePattern": "^.{14}LT.*$", "minimumNumberOfAttributes": 19, "expectedNumberOfAttributes": 19, "recordLength": 117, "attributes": [ { "attributeIdentifier": 1, "attributeName": "PROVIDER TYPE", "attributeMaximumLength": 1, "datatype": { "datatypeName": "AN" } } ] } ]}; line: 1, column: 4]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1071)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)
at Json_Parser_Jackson$.main(Json_Parser_Jackson.scala:33)
at Json_Parser_Jackson.main(Json_Parser_Jackson.scala)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

程序

import java.io.{File, StringWriter}

import java.util

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.avro.ipc.specific.Person

import scala.io.Source


case class Definition(recordDefinitions: Seq[RecordDefinitionsClass])
case class RecordDefinitionsClass(recordDefinitionIdentifier:Int,recordTypeCode: String,recordTypePattern:String,minimumNumberOfAttributes: Int,expectedNumberOfAttributes: Int,recordLength:Int,attributes: Seq[Attributes])
case class Attributes(attributeIdentifier: Int,attributeName:String,attributeMaximumLength:Int,datatype: Seq[DataType])
case class DataType(datatypeName:String)

object Json_Parser_Jackson {
def main(args: Array[String]): Unit = {
val fileContent = Source.fromFile("C:\\Users\\xxxnd\\ideaProject\\jsonparser\\src\\main\\resources\\Json_file.json","UTF-8").getLines.mkString

val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)

val person2: Definition = new ObjectMapper().readValue(fileContent,classOf[Definition])

println(person2)


}
}

Json 文件

{
"recordDefinitions": [
{
"recordDefinitionIdentifier": 2,
"recordTypeCode": "LT",
"recordTypePattern": "^.{14}LT.*$",
"minimumNumberOfAttributes": 19,
"expectedNumberOfAttributes": 19,
"recordLength": 117,
"attributes": [
{
"attributeIdentifier": 1,
"attributeName": "PROVIDER TYPE",
"attributeMaximumLength": 1,
"datatype": {
"datatypeName": "AN"
}
}
]
}
]
}

最佳答案

如果我没记错的话,Scala 案例类没有无参数构造函数,而是具有所有字段作为参数的构造函数以及伴随对象的 apply 方法。然而,Jackson 对象映射器需要一个无参数构造函数来首先实例化该类。因此,最“ jackson ”的方法是创建一个 Java POJO,而不是像下面这样的案例类:Jackson Github

我建议你使用这样的东西: Implicit JSON conversion

另请参阅this answer.

关于java - 找不到类型[简单类型,类定义]的合适构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com