Search in sources :

Example 1 with BeanWithAppend

use of com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend in project tutorials by eugenp.

the class ExtraAnnotationUnitTest method whenUsingJsonAppendAnnotation_thenCorrect.

@Test
public void whenUsingJsonAppendAnnotation_thenCorrect() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    BeanWithAppend bean = new BeanWithAppend(2, "Bean With Append Annotation");
    ObjectWriter writer = mapper.writerFor(BeanWithAppend.class).withAttribute("version", "1.0");
    String jsonString = writer.writeValueAsString(bean);
    assertThat(jsonString, containsString("version"));
    assertThat(jsonString, containsString("1.0"));
}
Also used : BeanWithAppend(com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

BeanWithAppend (com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1