简体   繁体   English

忽略来自Rest API Response Java的Null值字段

[英]Ignore Null Value Fields From Rest API Response Java

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. 在我的项目中,我向Rest Advance Client发送Rest Response它只显示具有一些值的字段和忽略(不显示)具有NULL值或空值的字段。

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(); 响应示例return Response.ok(firstResponse).build();

Advance rest client From web project : 高级休息客户端从Web项目:

{
  "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: 现在当我将其保存为DB中的Json String或当我想将其打印到控制台时,它还会打印带有null或空值的fiels:

{
  "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 ie i dont want to print null or empty value field i just want to ignore them. 我如何打印或保存此JSON字符串与休息客户端中的响应相同,即我不想打印null或空值字段我只想忽略它们。

in top og entity class , try with the annotation 在top og实体类中,尝试使用注释

@JsonInclude(Include.NON_EMPTY)

this annotation don't show any empty field in your json. 此注释不会在json中显示任何空字段。

Not giving you a code but here are some pointers for you: 没有给你一个代码,但这里有一些指示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM