实体类序列化JSON时,过滤掉为空的字段的方法

         在做一个接口返回数据时,其中一组数据的某个值为null,需要屏蔽掉这个字段,但是其他数据内的字段照常显示,考虑到两个方向,一个是在代码中,把为空的数据字段过滤掉,另一个是通过相应的注解来在实体类层面解决此问题。
         考虑到本人比较懒,想找个比较简便的方法,最后还是选择了使用Jackson的一个注解来完成此操作。
       @JsonInclude(Include.NON_NULL),此注解的作用就是实体转json,属性值为NULL或者为空时,该属性不参与序列化。
       将该标记放在属性上,如果该属性为NULL则不参与序列化,如果放在类上边,那对这个类的全部属性起作用。

//Include.Include.ALWAYS 默认 
//Include.NON_DEFAULT 属性为默认值不序列化 
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化 
//Include.NON_NULL 属性为NULL 不序列化 


GitHub 加速计划 / js / json
53
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:17 天前 )
230bfd15 Bumps [actions/dependency-review-action](https://github.com/actions/dependency-review-action) from 4.6.0 to 4.7.0. - [Release notes](https://github.com/actions/dependency-review-action/releases) - [Commits](https://github.com/actions/dependency-review-action/compare/ce3cf9537a52e8119d91fd484ab5b8a807627bf8...38ecb5b593bf0eb19e335c03f97670f792489a8b) --- updated-dependencies: - dependency-name: actions/dependency-review-action dependency-version: 4.7.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 5 天前
e9391dc5 Bumps [lukka/get-cmake](https://github.com/lukka/get-cmake) from 4.0.1 to 4.02. - [Release notes](https://github.com/lukka/get-cmake/releases) - [Commits](https://github.com/lukka/get-cmake/compare/57c20a23a6cac5b90f31864439996e5b206df9dc...ea004816823209b8d1211e47b216185caee12cc5) --- updated-dependencies: - dependency-name: lukka/get-cmake dependency-version: '4.02' dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 7 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐