且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Jackson定义可选的json字段

更新时间:2022-03-30 22:16:04

在杰克逊你无法区分可选和非可选字段。只需声明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.