How to add @JsonIgnore annotated fields in serializing in Jackson ObectMapper

后端 未结 4 968
时光说笑
时光说笑 2021-01-28 23:15

I need to add @JsonIgnore annotated fields while serializing an object by Jackson ObjectMapper. I know you may offer me to remove the @JsonIgnore

相关标签:
4条回答
  • 2021-01-28 23:52
    public class MainProgram {
        @JsonFilter("nameRemoveFilter")
        public static class User{
    
            private String name;
    
            private String age;
    
            private String password;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getAge() {
                return age;
            }
    
            public void setAge(String age) {
                this.age = age;
            }
    
            public String getPassword() {
                return password;
            }
    
            public void setPassword(String password) {
                this.password = password;
            }
    
    
    
        }
    
        public static void main(String[] args) {
            ObjectMapper mapper = new ObjectMapper();
            FilterProvider filters = new SimpleFilterProvider().addFilter("nameRemoveFilter",
                    SimpleBeanPropertyFilter.filterOutAllExcept("name","age"));
                // and then serialize using that filter provider:
                User user = new User();
                try {
                    String json = mapper.writer(filters).writeValueAsString(user);
                    System.out.println(json);
                } catch (JsonProcessingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    
    
        Works for Latest version of Jackson after 2.0
    
    0 讨论(0)
  • 2021-01-28 23:59

    You can define a SimpleBeanPropertyFilter and FilterProvider.

    First annotate your class with custom filter like this:

    @JsonFilter("firstFilter")
    public class MyDtoWithFilter {
    
        private String name;
    
        private String anotherName;
        private SecondDtoWithFilter dtoWith;
    
        // get set ....
    }
     @JsonFilter("secondFilter")
    public class SecondDtoWithFilter{
        private long id;
        private String secondName;
    }
    

    and this is how you will dynamically serialise your object.

        ObjectMapper mapper = new ObjectMapper();
    
        // Field that not to be serialised. 
        SimpleBeanPropertyFilter firstFilter = SimpleBeanPropertyFilter.serializeAllExcept("anotherName");
         SimpleBeanPropertyFilter secondFilter = SimpleBeanPropertyFilter.serializeAllExcept("secondName");
    
        FilterProvider filters = new SimpleFilterProvider().addFilter("firstFilter", firstFilter).addFilter("secondFilter", secondFilter);
    
        MyDtoWithFilter dtoObject = new MyDtoWithFilter();
        String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
    
    0 讨论(0)
  • 2021-01-29 00:08

    I would suggest removing and re-adding them programmatically via reflection when your specific mapping is happening.

    0 讨论(0)
  • 2021-01-29 00:15

    That suggests to me you have two different models with some common elements. I would reexamine your model.

    0 讨论(0)
提交回复
热议问题