Pink Spider/@JsonIgnore 와 JsonInclude

Created Tue, 08 Apr 2025 17:42:13 +0900 Modified Mon, 08 Dec 2025 08:41:47 +0900
257 Words 1 min

@JsonIgnore를 붙이면 해당 필드는 JSON 변환 시 무조건 제외됩니다. 즉, 값이 있든 없든 관계없이 JSON 결과에 필드 자체가 포함되지 않습니다.

예시로 보면:

public class User {
    private String name;

    @JsonIgnore
    private String password;

    // getters/setters
}

위 객체를 JSON으로 변환하면 (예: name="Alice", password="secret"이더라도):

{
  "name": "Alice"
}

password 필드는 무조건 빠집니다.


반면, 값이 없을 때만 필드를 제외하고 싶다면 @JsonInclude를 사용해야 합니다:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
    private String name;
    private String password;

    // getters/setters
}

이렇게 하면 passwordnull일 때만 JSON에서 제외됩니다.

필요에 따라 NON_EMPTY, NON_DEFAULT 같은 옵션도 사용할 수 있어요.