@jsonIgnore

Haechan Kim·2023년 5월 4일
0

Spring

목록 보기
39/68

@JsonIgnore : 객체 JSON으로 직렬화할 때 해당 필드를 무시하도록 지정하는 데 사용됨. 즉 데이터에 아예 포함 안됨.

Java 객체에는 불필요한 필드 JSON 직렬화에 포함하면 성능 저하, 전송된 데이터의 크기를 불필요하게 늘릴 수 있음. @JsonIgnore 어노테이션은 이러한 불필요한 필드를 무시하여 JSON 직렬화를 더 효율적으로 만들어준다.

@JsonIgnore 어노테이션은 필드 레벨과 메소드 레벨 모두에 사용 가능.

  • 필드 레벨
public class User {
    private Long id;
    
    @JsonIgnore
    private String password;

    // getter, setter, constructor, etc.
}

password 필드는 @JsonIgnore 어노테이션으로 표시되어 있기 때문에, JSON 직렬화 중에 해당 필드는 무시됨.

  • 메소드 레벨
public class User {
    private Long id;
    private String password;

    @JsonIgnore
    public String getPassword() {
        return password;
    }

    // getter, setter, constructor, etc.
}

getPassword() 메소드 -> JSON 직렬화 중에 해당 메소드가 반환하는 필드는 무시됨.

0개의 댓글