Search code examples
springjacksonannotations

default value corresponding to null key in jackson


Is there any annotation in jackson dependency in spring to all default value to those key who mapped to null value using jackson.

here is sample json example

{
 first-name : null,
 last-name : "somevalue"
}


will mapped to


{
 first-name : "defaultvalue"
 last-name : "somevalue"
}


Solution

  • There is no out of the box solution for it. You have multiple ways to tackle this, by using custom serializer or just using an if condition in the getter method.

    Something like this:

    public String getFirstName() {
            if (firstName == null)
                return "defaultvalue";
            return firstName;
    }