重载方法构造函数错误

时间:2016-11-12 21:17:21

标签: scala jackson

使用Jackson将json反序列化为类似乎我需要实现一个没有任何参数的默认构造函数,而且我的其他构造函数在所有属性中都放置了一个默认值

class Application(artistViewUrl: String="",
                  trackViewUrl: String="",
                  price: String="",
                  artworkUrl100: String=""
                 ) extends AppleBase {
  def this() {
    this("")
  }

}

没有默认值我有错误

Error:(14, 5) overloaded method constructor Application with alternatives:
  ()appleSearch.model.app.Application <and>
  (artistViewUrl: String,trackViewUrl: String,price: String,artworkUrl100: String)appleSearch.model.app.Application
 cannot be applied to (String)
    this("")

如果我删除非参数构造函数,Jackson会抛出此异常

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of politrons.apple.search.model.music.Album: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: java.io.StringReader@6aba2b86; line: 1, column: 3] (through reference chain: java.util.ArrayList[0])

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

类型检查器无法推断出要调用的正确构造函数。通过仅指定一个参数,任何构造函数都不匹配。您需要指定所有参数:this("", "", "", ""),或使用默认参数,如您的示例所示。