hello world

자바 Unrecognized field.. not marked as ignorable 본문

WEB/error_log

자바 Unrecognized field.. not marked as ignorable

sohyun_92 2021. 8. 31. 10:40
728x90

에러 :  Unrecognized field "ex1" (class ex.extest.dto.response.exResponseDTO), not marked as ignorable

 

json 데이터를 받아와서 dto 객체로 맵핑할때

dto 클래스에 선언되지 않은 속성(ex1)이

json에 있으면 오류가 발생한다.

알게된 해결 방법 3가지를 정리해보았다.3번째는 첨보는것,,기억해두기..


[ 해결 방법 3가지 ]

 

1.DTO Class에서 어노테이션 사용

@JsonIgnoreProperties(ignoreUnknown =true)  :  선언 필드 외에 모든 요소 제외

dto class 객체에 속성이 없으면 exception 발생하지않고 진행한다.

아래와 같이 사용한다.

@JsonIgnoreProperties(ignoreUnknown=true)
public class CouconApiResponseDTO { 
   private String vv;
   private String name;
}

 

2.DTO Class에서 어노테이션 사용

jsonIgnoreProperties({"제외하고자하는 특정 필드 NAME"})

jsonIgnoreProperties({"ex1"})
public class CouconApiResponseDTO { 
   private String vv;
   private String name;
}

 

3.ObjectMapper 사용시 옵션 설정 

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

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

 


Comments