저장된 객체의 필드값이 null일 경우에 생기는 문제가 있다.
이 객체에 대한 정보를 요청할 때 사용자는 null 이라는 정보를 확인하면 안되기 때문에 null로 저장되어있는 필드를 숨길 필요가 있다.
public class Product {
private String productName;
private Integer price;
private String description;
}
만약 description 필드의 값이 null 이라면
"data": {
"productName": "일회용손수건",
"price": 1000,
"description": null
}
이렇게 반환될것이다.
이번엔 @JsonInclude(JsonInclude.Include.NON_NULL)
어노테이션을 추가
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Product {
private String productName;
private Integer price;
private String description;
}
이 어노테이션은 Null 값이 아닌 경우에만 직렬화된 출력이 가능하도록 해준다.
"data": {
"productName": "일회용손수건",
"price": 1000
}
@JsonInclude
의 옵션은 다음과 같다.