Michael
Michael

Reputation: 115

How do you specify default values for Jackson deserialization

@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 the JSON above, I get the age property to be null.

I want Jackson to deserialize the empty fields ("" or null) in JSON with default values.

Like this:

{
    "username":"admin",
    "password":"000",
    "age":18
}

What should I do?

Upvotes: 5

Views: 24254

Answers (3)

Sunil Garg
Sunil Garg

Reputation: 602

Use the JsonSetter annotation with the value Nulls.SKIP

Value that indicates that an input null value should be skipped and the default assignment is to be made; this usually means that the property will have its default value.

As follows:

     public class User {
        @JsonSetter(nulls = Nulls.SKIP)
        private Integer Score = 1000;
        ...
    }

usually, if you don't use @JsonSetter(nulls = Nulls.SKIP) then the default value will be initialized if there is no value coming in JSON, but if someone explicitly put a null then it can lead to problem. Using @JsonSetter(nulls = Nulls.SKIP) will tell the Json de-searilizer to avoid null initialization.


Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

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>

Upvotes: 5

kevinjom
kevinjom

Reputation: 353

One approach is to make you setAge to handle the null case, for example:

 void setAge(Integer age){
   if(age!=null) this.age = age;
 }

Or you can have a look at @JsonSetter(nulls=Nulls.SKIP), I just looked at the source code, I haven't tried :(

Hope that helps.

Upvotes: 0

Related Questions