A Guide to @JsonIncludeProperties Annotation in Jackson (#13808)
This commit is contained in:
parent
95f844ffa4
commit
d964c3d1eb
@ -0,0 +1,18 @@
|
||||
package com.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
|
||||
|
||||
@JsonIncludeProperties({ "name" })
|
||||
public class BeanWithInclude {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public BeanWithInclude() {
|
||||
|
||||
}
|
||||
|
||||
public BeanWithInclude(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -213,6 +213,15 @@ public class JacksonAnnotationUnitTest {
|
||||
assertThat(result, not(containsString("id")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIncludeProperties_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithInclude bean = new BeanWithInclude(1, "My bean");
|
||||
final String result = new ObjectMapper().writeValueAsString(bean);
|
||||
assertThat(result, containsString("My bean"));
|
||||
assertThat(result, not(containsString("id")));
|
||||
assertThat(result, containsString("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonIgnore_thenCorrect() throws JsonProcessingException {
|
||||
final BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
|
||||
|
Loading…
x
Reference in New Issue
Block a user