简体   繁体   English

GSON排除具有自定义注释的字段

[英]GSON exclude fields with custom annotation

We have a jsp file with generic page content for admin purpose. 我们有一个具有通用页面内容的jsp文件,用于管理目的。

In this we have to hide audit columns, we have used annotation to specify the columns to show in search result, and we have one custom class that filters special character fields. 在这种情况下,我们必须隐藏审核列,使用注释来指定要在搜索结果中显示的列,并且我们有一个自定义类来过滤特殊字符字段。

 Gson gson = gsonBuilder.registerTypeHierarchyAdapter(Object.class,
        new CustomJsonSerializer())
        .serializeNulls()
        .setExclusionStrategies(new AdminTableSearchJsonStrategy())
        .create();

With the above code special character filters but hiding audit columns not working. 使用上述代码,特殊字符过滤器将隐藏审计列,但无法正常工作。

If we remove CustomJsonSerializer class then audit column exclusive working, but we want these two to be there, please advice this in case. 如果我们删除CustomJsonSerializer类,然后审核列的独占工作,但是我们希望这两个存在,请以防万一。

对要包含的字段使用@Expose Annotation,并使用GsonBuilder().excludeFieldsWithoutExposeAnnotation()构建

我创建了一个扩展ExclusionStrategyJsonSerializer的空接口,并且此接口在AdminTableSearchJsonStrategy中实现,现在此类具有两种功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM