杰克逊 - 必需的财产? [英] Jackson - Required property?

查看:41
本文介绍了杰克逊 - 必需的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象映射器上使用 Jackson 的 readValue() 方法来读取 JSON 文件并将其转换为我的 java 对象.

I'm using Jackson's readValue() method on an object mapper to read from a JSON file and convert it into my java object.

例如

mapperObject.readValue( node, MyTargetClass.class )

是否可以在 MyTargetClass 上设置任何注释以强制执行必需的属性?例如,如果我有一个具有 ABC、DEF 和 GHI 属性的 JSON 对象,并且我的 Json 如下

Are there any annotations that I can set on MyTargetClass to enforce required attributes? For example, if I have a JSON object with properties ABC,DEF and G and my Json is the following

{
  "ABC" : "somevalue"
  "DEF" : "someothervalue" 
}

我希望它以某种方式失败,并且只在包含 ABC、DEF 和 GHI 的 readValue 上成功.

I want it to fail somehow, and only succeed on the readValue if it contained ABC, DEF and GHI.

推荐答案

Jackson 不包括验证功能,这是设计使然(即被视为超出范围).但通常使用的是Bean Validation API 实现.这样做的好处是数据格式处理和验证逻辑之间的解耦.这就是像 DropWizard 这样的框架所使用的;这就是 JAX-RS(如 Jersey)为 JAX-RS 2.0 采取的方向.

Jackson does not include validation functionality, and this is by design (i.e. that is considered out-of-scope). But what is usually used is Bean Validation API implementation. The nice thing about this is decoupling between data format handling, and validation logic. This is what frameworks like DropWizard use; and it's the direction JAX-RS (like Jersey) are taking things for JAX-RS 2.0.

这篇关于杰克逊 - 必需的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆