Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import com.fasterxml.jackson.annotation.JsonView;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3.  
  4. class Views {
  5.  
  6. public static class Normal{}
  7.  
  8. public static class WithTitle extends Normal{}
  9.  
  10. }
  11.  
  12. class Staff {
  13.  
  14. @JsonView(Views.WithTitle.class)
  15. private String name;
  16. @JsonView(Views.Normal.class)
  17. private String title;
  18.  
  19. public Staff(String name, String title) {
  20. this.name = name;
  21. this.title = title;
  22. }
  23.  
  24. public String getName() {
  25. return name;
  26. }
  27.  
  28. public String getTitle() {
  29. return title;
  30. }
  31. }
  32.  
  33. class JacksonJsonViewExample {
  34.  
  35. public static void main(String[] args) throws Exception {
  36. Staff staff = new Staff("Joe Bloggs", "Mr");
  37. ObjectMapper mapper = new ObjectMapper();
  38.  
  39. // {"title":"Mr"}
  40. System.out.println(mapper.writerWithView(Views.Normal.class).writeValueAsString(staff));
  41. // {"name":"Joe Bloggs","title":"Mr"}
  42. System.out.println(mapper.writerWithView(Views.WithTitle.class).writeValueAsString(staff));
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement