How do you specify default values for Jackson deserialization
You can define a custom getter property where setting your default value in case of null
.
public Integer getAge() {
if (age == null) {
return 18;
} else {
return this.age;
}
}
Note that you can't change the setAge
method because it is not invoked in this case, infact there is no age
field that will inform Jackson to call that method.
An alternative is to use a custom constructor and use the JsonSetter
annotation with the value Nulls.SKIP
Value that indicates that an input null value should be skipped and no assignment is to be made; this usually means that the property will have its default value.
as follow:
public class User {
@JsonSetter(nulls = Nulls.SKIP)
private Integer age;
public User() {
this.age = 18;
}
...
}
The @JsonSetter
is present in package com.fasterxml.jackson.annotation
and can be imported as dependency in maven using
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>YOURVERSION</version>
</dependency>
Michael
Updated on June 04, 2022Comments
-
Michael almost 2 years
@ResponseBody @RequestMapping(value="/getUser") public JSONObject getContent(@ReqeustBody User user)
Up here is my Controller code.
@Data public class User{ private String username = "administrator"; private String password = "123456"; private Integer age = 18; }
Up here is my
User
class code.{ "username":"admin", "password":"000", "age":"" }
When I
POST
theJSON
above, I get theage
property to benull
.I want
Jackson
to deserialize the empty fields (""
ornull
) inJSON
with default values.Like this:
{ "username":"admin", "password":"000", "age":18 }
What should I do?
-
Arthur Eirich over 5 yearsSee here
-
Echoinacup over 5 yearsIt is strange that the age field is initialized but as far as I understand in the response it is missing
-
-
Davide Lorenzo MARINO over 5 yearsThe setAge doesn't work because there is no age field. If there was an age field with a null value it could work, but not in absence of the field age.
-
Michael over 5 yearsI didn't find the
JsonSetter
annotation that I could write on the field,com.fasterxml.jackson.annotation.JsonSetter
is only allowed onclass
andmethod
. -
Davide Lorenzo MARINO over 5 years@Michael it is present in the package com.fasterxml.jackson.annotation. Check the updated answer for details
-
Michael over 5 yearsThank you Davide, my version is 2.8.0, it can't be written on a
field
. -
kevinjom over 5 years@Michael correct, as the comment in the source code suggests
// ^^^ allowed on Fields, (constructor) parameters since 2.9
-
kevinjom over 5 years@DavideLorenzoMARINO Ah I just saw the
@Data
annotation from lombok ( I assume), so there is no setter there. This is actually a bit tricky, but its java's nature, even there is no setter, the client code can still change the object's field value by using reflection which I assume thats how jackson does it. I think your answer makes sense in the context that you still wanna use@Data
.