I am facing Nullpointer exception while trying to send a empty field. Need annotation approach. If else approach not required
I tried if else approach and it worked. But needed annotation approach to resolve the same.
I am facing Nullpointer exception while trying to send a empty field. Need annotation approach. If else approach not required
I tried if else approach and it worked. But needed annotation approach to resolve the same.
2
Answers
I assume you want to do a deserialization (is it the case ?)
For the jackson library and null values see Setting default values to null fields when mapping with Jackson
For empty field if it’s a string look at this property :
It replaces all empty string with null globally.
In Java, you can use the @JsonInclude annotation provided by the Jackson library to assign a default value to a field when the field provided in JSON is null or empty.
Here’s an example of how to use the @JsonInclude annotation:
In this example, the @JsonInclude annotation is used to specify that the field should be included in the JSON output only if its value is not null. The default value "default value" is set inside the default constructor of the class.
This way, when the field provided in the JSON is null or empty, the default value will be used for serialization without requiring an if-else approach in your code.