[Spring]@JsonIgnoreProperties

동민·2021년 6월 28일
0

JSON

{
  	"name": "kim",
  	"age": 15
}

DTO

public class Info {
	private String name;
}

JSON 데이터를 받아와서 객체로 매핑할 때, 클래스 필드(DTO)와 응답 데이터(JSON)가 맞지 않을 경우
아래와 같은 에러가 발생한다.

# Exception
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

예외를 피하려면 DTO에 아래의 어노테이션 작성

  1. 선언한 필드 외 모두 제외
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
public class Info {
	private String name;
}
  1. 특정 필드 제외
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties("age")
public class Info {
	private String name;
}
profile
BE Developer

0개의 댓글