쩨이엠 개발 블로그

@JsonIgnoreProperties - UnrecognizedPropertyException: Unrecognized field , not marked as ignorable (one known property) 본문

개발/JAVA

@JsonIgnoreProperties - UnrecognizedPropertyException: Unrecognized field , not marked as ignorable (one known property)

쩨이엠 2020. 4. 29. 12:55
728x90
반응형

 

Jackson databind 에러나는경우 UnrecognizedPropertyException이 발생한다

 

ObjectMapper objectMapper = new ObjectMapper();

Data data = objectMapper.readValue(info, Data.class);

이 때 Unrecognized field , not marked as ignorable (one known property) 에러가 났는데

내 경우에는 Data 객체 안에 info에 대한 필드가 존재 하지 않는 경우였다

 

파싱하고 싶은 데이터는

{“content”:{“id”:”sensorId_001”,”topic”:”sensor-data-test”},”datainfo”:{“reqTime”:1588126260, “reqId”:1588126395}}

였는데

Data 객체는 Content와 topic밖에 정의가 되어있지 않아 에러가 났다

@Getter
@Setter
public static class Data {

    private Content content;
    @Getter
    @Setter
    public static class Content {
    private String topic;
    }

}

 

이 땐 ignorable에 대한 어노테이션을 달아주면 된다

@JsonIgnoreProperties(ignoreUnknown = true)

 

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Data {

    private Content content;
    @Getter
    @Setter
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Content {
    private String topic;
    }

}

어노테이션을 달아주면 잘 돌아가는 것을 확인 할 수 있다

 

728x90
반응형
Comments