개발새발

class 객체에 null값인 객체 미노출 (@JsonInclude) 본문

Framework/springboot

class 객체에 null값인 객체 미노출 (@JsonInclude)

재래김유진 2021. 7. 29. 10:07
728x90
반응형

rMate 차트를 사용하면서 데이터를 불러올 때 객체에 null이거나 0인 필드명들이 노출이 되지 않아야 

차트에서 데이터를 정확하게 나타낼 수 있다. 

 

따라서, 불필요한 필드들의 null 값 리턴이나 의도적으로 미노출하고자 하는 class에 

@JsonInclude 어노테이션을 사용한다.

 

 

[@JsonInclude 미사용시 : 기본값]

public static class test {
	
    private int num;
    private String name;
    private String adress;
    private int phoneNum;
}
{
    "num" : 1,
    "name" : "지나",
    "adress" : null,
    "phoneNum" : 0
}

 

 

 

[옵션 종류]

 

@JsonInclude(JsonInclude.Include.NON_DEFAULT)

  • primitive 타입이 디폴트 값이면 제외한다. (int / Integer : 0 , boolean / Boolean : false 등)
  • Date의 timestamp가 0L이면 제외한다.
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public static class test {
	
    private int num;
    private String name;
    private String adress;
    private int phoneNum;
}
{
    "num" : 1,
    "name" : "지나",
    "adress" : null,
    "phoneNum" : 0
}

 

 

 

@JsonInclude(JsonInclude.Include.NON_NULL)

  • null은 제외한다. 
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class test {
	
    private int num;
    private String name;
    private String adress;
    private int phoneNum;
}
{
    "num" : 1,
    "name" : "지나",
    "phoneNum" : 0
}

 

 

이 외에도 다른 옵션들이 있다.

공식 문서 : https://fasterxml.github.io/jackson-annotations/javadoc/2.9/com/fasterxml/jackson/annotation/JsonInclude.Include.html

728x90
반응형

'Framework > springboot' 카테고리의 다른 글

[Spring Boot에 lombok추가]  (0) 2019.12.12
Comments