1

In my project when i send Rest Response to Advance Rest Client It only shows Fields which Have some values and Ignores(Does not show) fields which have NULL Values or Empty values.

Part Of Code:

Gson gson=new Gson();
// firstResponse is Object which contains the values
String jsonString = gson.toJson(firstResponse);
test.saveJson(jsonString); //OR System.out.println(jsonString);            
return Response.ok(firstResponse).build(); // Response to Rest Client

Response sample To return Response.ok(firstResponse).build();

Advance rest client From 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 i save this as Json String in DB or When i want to Print this to console it also prints the fiels with 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 i print or save this JSON string same as response in rest client i.e. i dont want to print null or empty value field i just want to ignore them.

4
  • I suspect it is the translation of this JSON string to your Java object that is adding these values just before saving in database. Commented Aug 26, 2013 at 10:34
  • how to resolve this ?? Commented Aug 26, 2013 at 10:39
  • First verify by querying the DB directly whether this is indeed the case. If that is true then post your domain class for this JSON as well. Commented Aug 26, 2013 at 10:41
  • i just want to print values without null fields Commented Aug 26, 2013 at 10:44

2 Answers 2

2

in top og entity class , try with the annotation

@JsonInclude(Include.NON_EMPTY)

this annotation don't show any empty field in your json.

Sign up to request clarification or add additional context in comments.

Comments

1

Not giving you a code but here are some pointers for you:

2 Comments

1st doesnt ignore null fields rather it shows null values i have a long response with lots of lists in there cant we have Response.ok(firstResponse).build() in db or console to simply ignore null fields or cant we convert this Response to json i tried with get entity but result was same
As I wrote: You may need to use a custom exclusion strategy and for example on that use linked Q&A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.