deserialisatio时自定义类上的jackson readvalue上的InvalidDefinitionException

时间:2018-06-13 06:47:05

标签: kotlin jackson

我在将json解析为我自己的自定义类时得到InvalidDefinitionException。     线程“main”中的异常com.fasterxml.jackson.databind.exc.InvalidDefinitionException:类型id.or.jisedu.repo.Book的类型定义无效:构造函数的参数#0 [id.or.jisedu.repo.Book的构造函数,注释:[ null]]没有属性名称注释;当多参数构造函数注释为Creator时,必须具有名称  在[来源:(字符串)“......”

以下是造成问题的一行:

val wrapper: BookWrapper = mapper.reader().forType(BookWrapper::class.java).readValue("""
{
  "book": {
    "publisher": "Pearson",
    "image": "https:\/\/images.isbndb.com\/covers\/34\/13\/9780134093413.jpg",
    "title_long": "Campbell Biology (11th Edition)",
    "edition": "11",
    "date_published": "2016-10-29",
    "authors": [
      "Lisa A. Urry",
      "Michael L. Cain",
      "Steven A. Wasserman",
      "Peter V. Minorsky",
      "Jane B. Reece"
    ],
    "title": "Campbell Biology (11th Edition)",
    "isbn13": "9780134093413",
    "binding": "Hardcover",
    "publish_date": "2016-10-29",
    "isbn": "0134093410"
  }
}""")

这是我的bookwrapper课程:

data class BookWrapper(@JsonProperty("book") val book: Book)

@JsonInclude(JsonInclude.Include.NON_NULL)
data class Book(
  val title: String,
  @JsonProperty("title_long")
  val titleLong: String? = null,
  val isbn: String? = null,
  val isbn13: String? = null,
  val dewey_decimal: String? = null,
  val format: String? = null,
  val publisher: String? = null,
  val publish: String? = null,
  val language: String? = null,
  @JsonProperty("date_published")
  val datePublished: String? = null,
  @JsonProperty("publish_date")
  val publishDate: String? = null,
  val edition: String? = null,
  val pages: String? = null,
  val binding: String? = null,
  val dimensions: String? = null,
  val overview: String? = null,
  val excerpt: String? = null,
  val synopsys: String? = null,
  val image: String? = null,
  val authors: List<String>,
  val subjects: List<String>? = null,
  var id: Long? = null,
  var amount: Int = 1
)

修改

我忘了注册KotlinModule。别忘了这样做。

1 个答案:

答案 0 :(得分:0)

是的,注册 KotlinModule 会有所帮助。注册 Kotlin 模块:

  1. 为 build.gradle 添加依赖:

    CameraStream

  2. 创建对象映射器:

    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8") import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

更多信息请访问 https://www.baeldung.com/kotlin/jackson-kotlin