コード
data class CustomClass(
JsonProperty("id", required = false)
val id: Int,
JsonProperty("name", required = true)
val name: String
)
fun test() {
val mapper = ObjectMapper().configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true)
var raiseError = false
try {
var customClass = mapper.readValue("{\"id\":10}", javaClass<CustomClass>())
} catch (e: JsonMappingException) {
raiseError = true
}
assertTrue(raiseError)
}
こんな感じで、 JsonProperty(required = true)
の AnnotationをつけたfieldはExceptionを投げてくれる。
便利!