- 杰克逊必需属性? [英] Jackson - Required property?

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

问题描述

我使用上的一个对象映射器杰克逊的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.

推荐答案

杰克逊不包括验证功能,而这是由设计(即被认为是超出范围)。但是,通常采用的是 Bean验证API 实现。
这个做的好处是数据格式处理和验证逻辑之间的解耦。
这就好比DropWizard使用什么框架;和它的方向JAX-RS(如新泽西州)正在东西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天全站免登陆