본문 바로가기
▶ Back-End/Java

JSON Unrecognized field 해결방법(@JsonIgnoreProperties)

by 오늘도 코딩 2021. 3. 3.
728x90
반응형

JSON 데이터를 구성하는 요소가 가변적일 때 무시하는 방법

*소스는 변하지 않았지만 연동 했던 API 응답 값이 변했다고 가정

 

 

▷ ERROR

- JSON 데이터를 매핑하지 못해 에러 발생

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

 

 

▷ 해결 방법

*2가지 중에 골라서 사용

 

- Example JSON data

{
  "id": 1,
  "name":"test"
}

 

 

① 클래스 레벨에 @JsonIgnoreProperties 어노테이션

- 특정 요소 제외

 

@JsonIgnoreProperties({"name"})
public class TestClass{
	String id;
}

 

 

- 선언한 필드 이외 모든 요소 제외

@JsonIgnoreProperties(ignoreUnknown = true)
public class TestClass{
	String id;
}

 

 

② ObjectMapper 객체 생성 옵션 

- DeserializationFeature의 다른 옵션도 존재

 

ObjectMapper objectMapper = 
		new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

 

 

728x90
728x90

댓글