반응형
VO객체를 JSON으로 변환할 때 포함되면 안되는 변수에 @JsonIgnore 어노테이션을 선언하여 JSON에 포함되지 않도록 할 수 있다.
하지만 상황에 따라 json 포함 여부가 달라지는 경우 아래와 같은 방법을 사용하면 된다.
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //null인 데이터를 제외하고 json 생성
mapper.writeValueAsString(testVO);
VO에 default로 적용하려면 VO Class에 @JsonInclude 어노테이션을 선언하면 된다.
@JsonInclude(JsonInclude.Include.NON_NULL)
1. JsonInclude.Include.ALWAYS
- 모든 데이터를 JSON으로 변환한다.
2. JsonInclude.Include.NON_NULL
- null인 데이터는 제외한다.
3. JsonInclude.Include.NON_ABSENT
- null인 데이터와
- 참조 유형 (Java 8 'Optional'또는 {link java.utl.concurrent.atomic.AtomicReference})의 "absent"값은 제외한다.
예를 들어 TestVO에 private AtomicReference<String> ar; 가 선언되어 있고,
testVO.setAr(new AtomicReference<String>()); 만 했을 경우 제외된다.
4. JsonInclude.Include.NON_EMPTY
- null인 데이터와
- absent 데이터와
- Collections, Map의 isEmpty()가 true인 데이터와
- Array의 length가 0인 데이터와
- String의 length가 0인 데이터는 제외한다.
5. JsonInclude.Include.NON_DEFAULT
- empty인 데이터와
- 기본형 타입이 default값인 데이터와 (예: int/Intege=0, boolean=true)
- Date의 timestamp가 0L인 데이터는 제외한다.
728x90
반응형
댓글