JsonView
public class Views {
public static class Public {
}
}
public class User {
public int id;
@JsonView(Views.Public.class)
public String name;
}@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:
Last updated