json - Ignore Null Value Fields From Rest API Response Java -
in project when send rest response advance rest client shows fields have values , ignores(does not show) fields have null values or empty values.
part of code:
gson gson=new gson(); // firstresponse object contains values string jsonstring = gson.tojson(firstresponse); test.savejson(jsonstring); //or system.out.println(jsonstring); return response.ok(firstresponse).build(); // response rest client
response sample return response.ok(firstresponse).build();
advance rest client web project :
{ "name": "smith", "properties": { "propertylist": [ { "id": "072", "number": "415151", "address": "somewhere" }, { "id": "151", "number": "a800cc79-99d1-42f1-aeb4-808087b12c9b", "address": "ninink" }, { "id": "269", }, ], }, }
now when save json string in db or when want print console prints fiels null or empty values:
{ "name": "smith", "properties": { "propertylist": [ { "id": "072", "number": "415151", "address": "somewhere" }, { "id": "151", "number": "a800cc79-99d1-42f1-aeb4-808087b12c9b", "address": "ninink" }, { "id": "269", "number": "", "address": "" }, ], }, "resultlist" :[] }
how can print or save json string same response in rest client i.e. dont want print null or empty value field want ignore them.
not giving code here pointers you:
read manual:
how handle null values
you may need use
custom exclusion strategy
also read q&a: gson: how exclude specific fields serialization without annotations
Comments
Post a Comment