Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Jackson ObectMapper中的序列化中添加@JsonIgnore注释字段_Java_Json_Serialization_Jackson - Fatal编程技术网

Java 如何在Jackson ObectMapper中的序列化中添加@JsonIgnore注释字段

Java 如何在Jackson ObectMapper中的序列化中添加@JsonIgnore注释字段,java,json,serialization,jackson,Java,Json,Serialization,Jackson,我需要添加@JsonIgnore注释字段,同时通过Jackson ObjectMapper序列化对象。我知道您可能会建议我从类中删除@JsonIgnore注释,但我需要它们在我的应用程序的某些部分是可忽略的。在我的应用程序的另一部分中,我需要在我的json字符串中有那些@JsonIgnore注释字段 我建议在发生特定映射时通过反射以编程方式删除和重新添加它们。我建议在发生特定映射时通过反射以编程方式删除和重新添加它们。这向我建议,您有两个不同的模型,其中包含一些公共元素。我会重新检查你的模型。这

我需要添加
@JsonIgnore
注释字段,同时通过
Jackson ObjectMapper
序列化对象。我知道您可能会建议我从类中删除
@JsonIgnore
注释,但我需要它们在我的应用程序的某些部分是可忽略的。在我的应用程序的另一部分中,我需要在我的
json
字符串中有那些
@JsonIgnore
注释字段

我建议在发生特定映射时通过反射以编程方式删除和重新添加它们。

我建议在发生特定映射时通过反射以编程方式删除和重新添加它们。

这向我建议,您有两个不同的模型,其中包含一些公共元素。我会重新检查你的模型。

这对我来说意味着你有两个不同的模型,有一些共同的元素。我将重新检查您的模型。

您可以定义SimpleBeanPropertyFilter和FilterProvider

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
首先,使用自定义筛选器对类进行注释,如下所示:

@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;
}
这就是动态序列化对象的方式

    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);

您可以定义SimpleBeanPropertyFilter和FilterProvider

首先,使用自定义筛选器对类进行注释,如下所示:

@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;
}
这就是动态序列化对象的方式

    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);

也许考虑使用杰克逊模块?也许考虑使用杰克逊模块?谢谢你的答案。如何在serializeAllExcept中添加子字段?我的意思是我有一个类,这个类的一个属性是另一个类。这个类还有一些字段应该序列化还是不序列化,在这种情况下,您必须将完整路径添加到属性中。正在为此更新上面的应答代码。快乐编码!!:)不幸的是,它没有起作用。我仍然可以在我的json字符串中看到innerProperty2。但是attribute1不是json格式的。@gabby请用嵌套属性的工作代码编辑我的答案。它会帮助别人。谢谢你的回答。如何在serializeAllExcept中添加子字段?我的意思是我有一个类,这个类的一个属性是另一个类。这个类还有一些字段应该序列化还是不序列化,在这种情况下,您必须将完整路径添加到属性中。正在为此更新上面的应答代码。快乐编码!!:)不幸的是,它没有起作用。我仍然可以在我的json字符串中看到innerProperty2。但是attribute1不是json格式的。@gabby请用嵌套属性的工作代码编辑我的答案。它会帮助别人。谢谢