在我的数据类中,我有
data class Example(
val purchaseId: String,
val customerId: String,
val purchaseDate: Long
)
将有效负载映射到示例时,我有
val expectedValue = "{\"purchaseId\":\"5005d000023FqKXAA0\",\"customerId\":\"10065416831\",\"purchaseDate\":1687523394\"}"
val result = mapper.readValue(expectedValue, Example::class.java)
因此,当预期值具有所有属性时,预期值会毫无问题地映射到示例。
但是当预期值缺失时
purchaseId
或其他字段如
val expectedValue = "{\"customerId\":\"10065416831\",\"purchaseDate\":1687523394\"}"
我明白了
Instantiation of [simple type, class Example] value failed for JSON property purchaseId due to missing (therefore NULL) value for creator parameter caseId which is a non-nullable type
在不使示例字段可为空的情况下,有没有办法可以验证预期的有效负载是否具有我关心的属性?
您需要通过以下方式初始化您的ObjectMapper:
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
您正在寻找的是 FAIL_ON_UNKNOWN_PROPERTIES 配置属性。