Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...