Search in sources :

Example 1 with BeanWithFilter

use of com.baeldung.jackson.annotation.BeanWithFilter in project tutorials by eugenp.

the class JacksonAnnotationUnitTest method whenSerializingUsingJsonFilter_thenCorrect.

@Test
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
    final BeanWithFilter bean = new BeanWithFilter(1, "My bean");
    final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
    final String result = new ObjectMapper().writer(filters).writeValueAsString(bean);
    assertThat(result, containsString("My bean"));
    assertThat(result, not(containsString("id")));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) BeanWithFilter(com.baeldung.jackson.annotation.BeanWithFilter) Matchers.containsString(org.hamcrest.Matchers.containsString) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

BeanWithFilter (com.baeldung.jackson.annotation.BeanWithFilter)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1