프로그래밍/ErrorLog

[자바, 스프링부트] Unrecognized field ,not marked as ignorable

미냐님 2023. 4. 26. 10:56
728x90
반응형

Json 을 Dto(객체)로 매핑할 때 Dto에 명시되지 않은 변수(속성)이 전달받은 Json 데이터에 들어있으면 발생

 

방법은 여러가지가 있으나 대표적 3가지가 있습니다.

 

1. ObjectMapper 를 설정하여 사용. Bean으로 등록하여 사용할 수도 있습니다.

@Configuration
public class ObjectMapperConfig {
    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        return mapper;
    }
}

 

2. @JsonIgnoreProperties(ignoreUnknown =true)

Json데이터를 매핑시키려는 Dto 클래스에 선언해서 사용

@JsonIgnoreProperties(ignoreUnknown=true)
public class ResponseDto { 
   private String id;
   private String name;
   private String type;
}


2. @JsonIgnoreProperties({"특정 변수 지정"})

특정 변수들만 지정하여 사용

@JsonIgnoreProperties({"type"})
public class ResponseDto { 
   private String id;
   private String name;
   private String type;
}

 

 

728x90