【问题标题】:Jackson JSON Ignore Properties DynamicallyJackson JSON 动态忽略属性
【发布时间】:2013-03-24 06:55:15
【问题描述】:

我有以下课程 -

员工

公共类员工{ 私人字符串名; 私人字符串姓氏; 私人字符串电子邮件地址; 私人字符串 ssn ; }

工资单

公共类工资{ // 不同的工资单相关字段 私人雇员 emp ; }

人力资源

公共类人力资源{ // 不同的 HR 相关字段 私人雇员 emp ; }

现在当我序列化我的 Payroll 类时,我不想从 Employee 类中序列化我的 ssn 字段。

当我序列化我的 HR 类时,我不想序列化 Employee 类中的 emailAddress 字段。

如何使用 Jackson JSON API 从序列化中动态排除字段?

【问题讨论】:

    标签: json jackson


    【解决方案1】:

    如何使用 Jackson JSON API 从序列化中动态排除字段?

    这似乎是申请JacksonJsonViews 的主要候选人。

    public class Employee {
    
        private String firstName;
        private String lastName;
        @JsonView(Views.Payroll.class) private String emailAddress; 
        @JsonView(Views.HR.class) private String ssn;
    }
    
    public class Payroll {
        // snip
    
        @JsonView(Views.Payroll.class)
        private Employee emp;
    }
    
    public class HR {
        // snip
    
        @JsonView(Views.HR.class)
        private Employee emp;
    }
    

    【讨论】:

    • 如果还有其他类,比如以 Employee 作为对象的 Department,并且在序列化时想要序列化 ​​Employee 中的所有内容,它是如何工作的?
    • 链接的文档涵盖了这一点。在这种情况下,您可以完全禁用视图处理。
    • 有没有办法根据运行时确定的某个值来选择使用哪些视图并进行更改?如果不是,那么这并不能回答问题的“动态”部分。
    【解决方案2】:

    我在下面回答以与客户阅读器动态反序列化。序列化时可以与 writer 做类似的事情以忽略。

    Jackson Change JsonIgnore Dynamically

    【讨论】:

      【解决方案3】:

      您可以在 JsonFilter 的帮助下实现这一点。

      1. 这样注释您要过滤的类:
      @JsonFilter("employeeFilter")
      public class Employee {
      
          private String firstName ;
          private String lastName ;
          private String emailAddress ; 
          private String ssn ;
      }
      
      1. 创建一个名为 FilterBeanService(或其他任何东西)的服务
      @Service
      public class FilterBeanService {
      
          // fields is an array of field names you wish not to sned in your response
          // beanFilterName is value you give when you annotage your bean class
          // dataSet is the data you want to filter
          public static MappingJacksonValue filterBean(String[] fields, String beanFilterName, Object dataSet ) {
              SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAllExcept(fields);
              FilterProvider filterProvider = new SimpleFilterProvider().addFilter(beanFilterName, filter);
              MappingJacksonValue jacksonValue = new MappingJacksonValue(dataSet);
              jacksonValue.setFilters(filterProvider);
              return jacksonValue;
          }
      }
      
      1. 然后,您可以在控制器中过滤不需要的文件
      @GetMapping("/whatever")
      public ResponseEntity<MappingJacksonValue> getSomething(){
         List<Employee> employees = eventRepo.findAll();
         String[] fields = {"ssn"};
         MappingJacksonValue jacksonValue = FilterBeanService.filterBean(fields, "employeeFilter", employees);
         return jacksonValue;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-19
        • 2019-02-22
        • 2021-06-11
        • 1970-01-01
        • 2017-09-22
        • 2022-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多