[개발일지9] @JsonIgnore

김희주·2023년 1월 9일
0

개발일지

목록 보기
7/10
post-thumbnail

1. JsonIgnore 정의

  • @JsonIgnore을 붙이면
    데이터를 주고 답을 때 해당 데이터는 'Ignore'되어서 아래의 결과창처럼 응답값에 보이지 않게 된다.(출처)
public class Login {
	
	private String uId;
	@JsonIgnore
	private String uPw; // pw 숨겨짐

2. @JsonIgnore & JsonProperty & @JsonIgnoreProperties & @JsonIgnoreType

  • @JsonIgnore 어노테이션은 클래스의 속성(필드, 멤버변수) 수준에서 사용됨
  • @JsonIgnoreProperties 어노테이션은 클래스 수준(클래스 선언 바로 위에)에 사용됨
  • @JsonIgnoreType 어노테이션은 클래스 수준에서 사용되며 전체 클래스를 무시함

2-1. @JsonProperty

  • 명시적 선언 그리고 JSON에서 선언된 키값을 맵핑시킴
    ex)
@JsonProperty("Name")
private String myName;
  • @JsonIgnore와 함께 사용할 수 있음
    ex)
@JsonIgnore
@JsonProperty("Name")
private String myName;

2-2. @JsonIgnoreProperties

  • 클래스 단위 레벨에 사용되며 지정된 필드값의 JSON 직렬, 역직렬화를 무시할 수 있음
@JsonIgnoreProperties({ "bookName", "bookCategory" })
public class Book {
	@JsonProperty("bookId")
	private String id;
    
	@JsonProperty("bookCategory")
	private String category;

2-3. @JsonIgnoreType

  • 클래스 레벨에 사용되며
    위에 클래스내에 존재하는 모든 필드들이 JSON 직렬, 역직렬화에 무시(즉 클래스 자체를 JSON 데이터 맵핑에 사용불가)
  • 마찬가지로 @JsonIgnoreType(false) 처럼 boolean 속성을가지고 있으며
    활성 / 비활성화 할 수 있음 default 값 = true
@JsonIgnoreType
class Address{
	private String address;
}

public class Writer {
    @JsonProperty("writerAddress")
    private Address address;
}

+그외 정보

profile
백엔드 개발자입니다 ☘

0개의 댓글