Close

Jackson JSON - Using @JsonView Annotation On Class level to specify default view

[Last Updated: Aug 23, 2018]

In the last tutorial we saw how to use @JsonView annotation on properties. This annotation can also be used on class level to indicate the default view(s), unless overridden by per-property annotation.

Example

Views

public class Views {
  public interface QuickContactView {}
  public interface DetailedView{}
}

POJO

@JsonView(Views.DetailedView.class)
public class Customer {
  @JsonView({Views.QuickContactView.class, Views.DetailedView.class})
  private String name;
  private String address;
  @JsonView({Views.QuickContactView.class})
  private String phone;
  @JsonView(Views.QuickContactView.class)
  private String cellPhone;
  private String emailAddress;
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd", timezone = "America/Chicago")
  private Date customerSince;
    .............
}

Serializing QuickContactView

public class MainQuickContactViewSerialization {
  public static void main(String[] args) throws IOException {
      Customer customer = new Customer();
      customer.setName("Emily");
      customer.setAddress("642 Buckhannan Avenue Stratford");
      customer.setPhone("111-111-111");
      customer.setCellPhone("222-222-222");
      customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
      customer.setEmailAddress("emily@example.com");
      System.out.println("-- before serialization --");
      System.out.println(customer);

      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writerWithView(Views.QuickContactView.class)
                   .writeValueAsString(customer);
      System.out.println("-- after serialization --");
      System.out.println(jsonString);
  }
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Mon Aug 23 18:59:33 CDT 2010}
-- after serialization --
{"name":"Emily","phone":"111-111-111","cellPhone":"222-222-222"}

Serializing DetailedView

public class MainDetailedViewSerialization {
  public static void main(String[] args) throws IOException {
      Customer customer = new Customer();
      customer.setName("Emily");
      customer.setAddress("642 Buckhannan Avenue Stratford");
      customer.setPhone("111-111-111");
      customer.setCellPhone("222-222-222");
      customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
      customer.setEmailAddress("emily@example.com");

      System.out.println("-- before serialization --");
      System.out.println(customer);

      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writerWithView(Views.DetailedView.class)
                   .writeValueAsString(customer);
      System.out.println("-- after serialization --");
      System.out.println(jsonString);
  }
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Mon Aug 23 19:06:05 CDT 2010}
-- after serialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","emailAddress":"emily@example.com","customerSince":"2010/08/23"}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.5.4

@JsonView on class example Select All Download
  • jackson-json-view-annotation-on-class
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Customer.java

    See Also