Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jackson - How to ignore @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)

I have a pojo, and most of the time I don't want to write a field to Json, but for a specific contition I want to write this value to json. For example my model is;

public class Person {

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    public String value;
}

Is it possible to configure Object Mapper to ignore @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)

question from:https://stackoverflow.com/questions/65939851/how-to-ignore-jsonpropertyaccess-jsonproperty-access-write-only

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Honestly I do not know of a quite direct way to really let the ObjectMapper ignore a particular annotation but why not using @JsonViews?

Just define some views…

public class Views {
    public static class Public {
    }
    public static class Internal {
    }
}

…put it on your conditional field…

public class Person {
    @JsonView(Views.Internal.class)
    public String value;
}

…and serialize it with your mapper…

    String result = mapper
      .writerWithView(Views.Public.class)
      .writeValueAsString(person);

The samples above were taken from good old Baeldung where you can find even more nice variants of this solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...