handle null from json with jackson

when jackson deserialize the json into object, some of the optional fields (non `@Notnull`) could get a null value assigned, something like

//JSON
{
 ...
 "field1": null
 ...
}

which would result into

class Payload{

  String field1;
}

where when the payload object being used, it could throw out NPE if the field1 is not handled properly.

The way to handle this properly is through setting a default value during deserialization, something like

class Payload{

  @JsonSetter(nulls=Nulls.SKIP)
  String field1="DEFAULT-NULL";
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s