JsonView

JSON Views can be used to serialize/deserialize objects.

http://www.baeldung.com/jackson-json-view-annotation

example:

https://stackoverflow.com/questions/38279782/what-is-the-json-view-class-in-jackson-and-how-does-it-work

public class Views {
    public static class Public {
    }
}

public class User {
    public int id;

    @JsonView(Views.Public.class)
    public String name;
}

By default - all properties not explicitly marked as being part of a view, are serialized. We are disabling that behavior with the handy DEFAULT__VIEW__INCLUSION feature.

@Test
public void whenUseJsonViewToSerialize_thenCorrect() 
  throws JsonProcessingException {

    User user = new User(1, "John");

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

    String result = mapper
      .writerWithView(Views.Public.class)
      .writeValueAsString(user);

    assertThat(result, containsString("John"));
    assertThat(result, not(containsString("1")));
}

JsonView is kinda of filter. Some fields would not be serialized.

example to be completed:

http://javasampleapproach.com/json/use-jsonview-serializede-serialize-customize-json-format-java-object

integrate Spring RestAPIs with @JsonView

http://javasampleapproach.com/java-integration/integrate-spring-restapis-jsonview

Last updated