简体   繁体   中英

Java Jackson org.codehaus.jackson.map.exc.UnrecognizedPropertyException

I am binding a JSON response to my class using Jackson. Everything works great except when there are more fields in my JSON response than my class defines. I want Jackson to ignore the fields that do not exist in my JSON response. This is due to compatability for future versions. If I add a new field I do not want previous versions of my client to crash.

Ideas?

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
        PromoResponse promoResponse = mapper.readValue(r, PromoResponse.class);

You can put the @JsonIgnoreProperties(ignoreUnknown=true) annotation on your PromoResponse class.

I believe you would want to do something like this after you declare your mapper object:

mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

-Dan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM