Jackson Annotation Examples

2020. 11. 17. 15:54OpenSource/Spring

반응형

@JsonInclude

어노테이션 속성을 제외 하는데 사용.

 

ex) JSONObject _connects가 null인 경우에 제외!

@JsonInclude(JsonInclude.Include.NON_NULL)
private JSONObject _connects;

 

@JsonIgnoreProperties

클래스 레벨의 어노테이션이고 무시할 속성을 표시

@JsonIgnoreProperties({ "id" })
public class BeanWithIgnore {
    public int id;
    public String name;
}

id는 무시되는걸 알수 있다.

@Test
public void whenSerializingUsingJsonIgnoreProperties_thenCorrect()
  throws JsonProcessingException {
 
    BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
 
    String result = new ObjectMapper()
      .writeValueAsString(bean);
    
    assertThat(result, containsString("My bean"));
    assertThat(result, not(containsString("id")));
}

@JsonIgnore

필드 레벨에서 무시할 속성을 표시

public class BeanWithIgnore {
    @JsonIgnore
    public int id;
 
    public String name;
}
@Test
public void whenSerializingUsingJsonIgnore_thenCorrect()
  throws JsonProcessingException {
 
    BeanWithIgnore bean = new BeanWithIgnore(1, "My bean");
 
    String result = new ObjectMapper()
      .writeValueAsString(bean);
    
    assertThat(result, containsString("My bean"));
    assertThat(result, not(containsString("id")));
}

 

아래의 참고 사이트에서 필요한 것들은 검색해서 습득하면 된다!

www.baeldung.com/jackson-annotations

 

Jackson Annotation Examples | Baeldung

The core of Jackson is basically a set of annotations - make sure you understand these well.

www.baeldung.com

 

반응형

'OpenSource > Spring' 카테고리의 다른 글

Spring Cloud Contract  (0) 2022.08.03
@Retryable  (0) 2020.11.17
@PreDestory란?  (0) 2020.11.12
@PostConstruct란?  (0) 2020.11.10
Spring WebFlux  (0) 2017.04.26