Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.fasterxml.jackson.annotation.JsonView;
- import com.fasterxml.jackson.databind.ObjectMapper;
- class Views {
- public static class Normal{}
- public static class WithTitle extends Normal{}
- }
- class Staff {
- @JsonView(Views.WithTitle.class)
- private String name;
- @JsonView(Views.Normal.class)
- private String title;
- public Staff(String name, String title) {
- this.name = name;
- this.title = title;
- }
- public String getName() {
- return name;
- }
- public String getTitle() {
- return title;
- }
- }
- class JacksonJsonViewExample {
- public static void main(String[] args) throws Exception {
- Staff staff = new Staff("Joe Bloggs", "Mr");
- ObjectMapper mapper = new ObjectMapper();
- // {"title":"Mr"}
- System.out.println(mapper.writerWithView(Views.Normal.class).writeValueAsString(staff));
- // {"name":"Joe Bloggs","title":"Mr"}
- System.out.println(mapper.writerWithView(Views.WithTitle.class).writeValueAsString(staff));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement