如何使用 Jackson 定义可选的 json 字段 [英] How to define optional json field using Jackson

查看:42
本文介绍了如何使用 Jackson 定义可选的 json 字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个可选字段的对象,但找不到合适的注释来对其进行建模.任何想法与杰克逊一起做的正确方法是什么?

I have an object with one optional field and can not find proper annotation to model it. Any ideas what is the proper way to do it with Jackson?

推荐答案

在 Jackson 中,您无法区分可选字段和非可选字段.只需在您的 POJO 中声明任何字段.如果您的 JSON 结构中不存在字段,则 Jackson 将不会调用 setter.您可以跟踪是否在 POJO 中使用标志调用了 setter.

In Jackson you cannot make the difference between optional and non-optional fields. Just declare any field in your POJO. If a field is not present in your JSON structure then Jackson will not call the setter. You may keep track of wether a setter has been called with a flag in the POJO.

这篇关于如何使用 Jackson 定义可选的 json 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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