在我的项目中,我将Rest Response发送到 Advance Rest Client 它只显示具有某些值的字段和忽略(不显示)具有NULL值或空值的字段。
部分代码:
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
回复示例至return Response.ok(firstResponse).build();
提升休息客户端来自网络项目:
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
},
],
},
}
现在当我将其保存为DB中的Json String或当我想将其打印到控制台时,它还会打印带有空值或空值的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" :[]
}
如何打印或保存此JSON字符串与rest客户端中的响应相同,即我不想打印null或空值字段我只想忽略它们。
答案 0 :(得分:2)
在top og实体类中,尝试使用注释
@JsonInclude(Include.NON_EMPTY)
此注释不会在json中显示任何空字段。
答案 1 :(得分:0)
没有给你一个代码,但这里有一些指示: