2

我有这个案例课

case class Contact(id: String, firstName: String, lastName: String)

当三个字段中的任何一个丢失时,我希望 Jackson Scala 对象映射器失败,例如:

val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.readValue[Contact](""""{"id":"123","firstName":"John"}""")

但是当在 Json 字符串中给出附加字段时成功,例如:

mapper.readValue[Contact](""""{"id":"123","firstName":"John","lastName":"Test","age":"24"}""")

我确实尝试过使用@JsonIgnoreProperties(ignoreUnknown = true)mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)但是这两个选项似乎都禁用了整个验证,而不仅仅是忽略了其他字段。

4

1 回答 1

2

@JsonProperty(required=true) would be the correct way to do this, but it is not implemented for deserialization yet.

Javadoc says:

Note that as of 2.0, this property is NOT used by BeanDeserializer: support is expected to be added for a later minor version.

Ticket for this Feature: https://github.com/FasterXML/jackson-databind/issues/230

I would check the required values myself right after deserialization but maybe someone else can come up with a workaround.

于 2013-07-29T14:04:49.413 回答