Ignore attributes when deserializing Jackson JSON in Java

In the example

Class Person{
   String name;
   int age;
}

If the JSON object is missing the attribute "age",

{
  name : John
}

Person person = objectMapper.readValue(jsonFileReader,Person.class);

It throws a jsonmappingexception indicating that it cannot be deserialized Are there comments that ignore missing fields during deserialization?

thank you

Solution

I think what you want is

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Person {
  ...
}

That's Jackson 1 X way I think in 2 There is a new way in X It's like

@JsonInclude(Include.NON_NULL)
public class Person {
  ...
}

These will tell Jackson to serialize only non - null values and not complain when deserializing missing values I think it will only set it to Java default

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>