I am trying to map some JSON objects to Java objects with Jackson. Some of the fields in the JSON object are mandatory(which I can mark with @NotNull
) and some are optional.
After the mapping with Jackson, all the fields that are not set in the JSON object will have a null value in Java. Is there a similar annotation to @NotNull
that can tell Jackson to set a default value to a Java class member, in case it is null?
Edit:
To make the question more clear here is some code example.
The Java object:
class JavaObject {
@NotNull
public String notNullMember;
@DefaultValue("Value")
public String optionalMember;
}
The JSON object can be either:
{
"notNullMember" : "notNull"
}
or:
{
"notNullMember" : "notNull",
"optionalMember" : "optional"
}
The @DefaultValue
annotations is just to show what I am asking. It's not a real annotation. If the JSON object is like in the first example I want the value of the optionalMember
to be "Value"
and not null
. Is there an annotation that does such a thing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…