Ignore missing properties during Jackson JSON deserialization in Java

前端 未结 6 1462
时光说笑
时光说笑 2020-12-03 06:14

In the example

class Person {
   String name;
   int age;
}

If the JSON object has a missing property \'age\',

{
  name :          


        
6条回答
  •  旧巷少年郎
    2020-12-03 07:13

    Annotation based approach is a better way for ignoring but If needed. The manual way of deserialization:

    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Person       person = mapper.readValue(jsonFileReader, Person.class);
    

提交回复
热议问题