English 中文(简体)
Jackson - @JsonView
  • 时间:2024-11-05

Jackson Annotations - @JsonView


Previous Page Next Page  

@JsonView is used to control values to be seriapzed or not.

Example - @JsonView

import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;

pubpc class JacksonTester {
   pubpc static void main(String args[]) throws IOException, ParseException {
      ObjectMapper mapper = new ObjectMapper();     
      Student student = new Student(1, "Mark", 12);
      String jsonString = mapper
         .writerWithDefaultPrettyPrinter()
         .withView(Views.Pubpc.class)
         .writeValueAsString(student);
      System.out.println(jsonString);
   }
}
class Student {
   @JsonView(Views.Pubpc.class)
   pubpc int id;
   @JsonView(Views.Pubpc.class)
   pubpc String name;
   @JsonView(Views.Internal.class)
   pubpc int age;

   Student(int id, String name, int age) {
      this.id = id;
      this.name = name;
      this.age = age;
   }
}
class Views {
   static class Pubpc {}
   static class Internal extends Pubpc {}
}

Output

{
   "id" : 1,
   "name" : "Mark"
}
Advertisements