java - give a default value for an attribute if the value is null in json by jackson -


suppose have class i.e.

private class student {         private integer x = 1000;          public integer getx() {             return x;         }          public void setx(integer x) {             this.x = x;         }     } 

now suppose json "{x:12}" , doing deserialization x have value 12. if json "{}" value of x = 1000 (get default value of attribute declared in class).

now if json "{x:null}" value of x becomes null here in case want value of x 1000. how via jackson. in advance.

i deserializing via below method, if helps in anyway: objectmapper.readvalue(<json string goes here>, student.class);

you should able override setter. add @jsonproperty(value="x") annotations getter , setter let jackson know use them:

private class student {     private static final integer default_x = 1000;     private integer x = default_x;      @jsonproperty(value="x")     public integer getx() {         return x;     }      @jsonproperty(value="x")     public void setx(integer x) {         this.x = x == null ? default_x : x;     } } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -