5
有一個具有默認參數的案例類Person。斯卡拉傑克遜對象映射器設置爲null,而不是默認值,如果類
傳遞映射器一個字符串缺少一個參數,映射器將其設置爲null。
期待它設置默認值
這是爲什麼?
例如:
@JsonIgnoreProperties(ignoreUnknown = true)
case class Person(id:Int,name:String="")
class MapperTest extends SpecificationWithJUnit {
"Mapper" should {
"use default param" in {
val personWithNoNameString = """{"id":1}"""
val mapper = new ObjectMapper();
mapper.registerModule(DefaultScalaModule)
val personWithNoName = mapper.readValue(personWithNoNameString,classOf[Person])
personWithNoName.name must be("")
}
}
}
得到錯誤:
'null' is not the same as ''
java.lang.Exception: 'null' is not the same as ''
不會編譯(預期詮釋了空) – Nimrod007 2014-10-01 11:59:45
更新的響應,null.asInstanceOf [INT]爲0,否則使用選項[Int] – 2014-10-01 13:36:58
仍然不能正常工作,運行測試會給出null – Nimrod007 2014-10-01 13:38:49