提问人:wheelerlc64 提问时间:8/16/2023 最后编辑:Anonymouswheelerlc64 更新时间:8/18/2023 访问量:55
在响应中将日期字段作为空发送,而不是设置为 null 且不在响应中发送
Send date field as empty in response instead of setting to null and not sending in response
问:
我目前正在处理一种情况,即在数据库中,我有一个设置了 NULL 值的日期列。当我测试下面的代码并获得我的响应时,当该值为 NULL 时,该字段甚至没有出现在我的响应中。如何自动发送响应中的字段并处理 NULL 方案,发送空值(如空字符串)?该字段是发送响应(userIndDate,一个 java.util.Date 对象)所必需的。我正在使用 jackson 来序列化数据。我没有包括我的存储库,它正在执行 db->object 映射以压缩代码。下面是代码片段?
var newResponse = SearchResponse.builder()
.item((Integer) row[0]).itemDesc((String) row[1])
.loc(String.valueOf((Integer ) row[2])).locDesc((String) row[3])
.ase((BigDecimal) (row[4]))
.aseStartDate((row[5] != null ? (java.util.Date)(row[5]) : null))
.indDate(row[6] != null ? (java.util.Date)(row[6]) : null)
.userIndDate((row[7] != null ? (java.util.Date)(row[7]) : null))
.seasonalProfileName((String) (row[8]))
.build();
resultStream.add(newResponse);
return newResponse;
当 userIndDate 列在数据库末尾为 NULL 时(即响应中缺少),响应如下:
{
"searchResponse": [
{
"loc": "1",
"item": 2,
"itemDesc": "someItemDesc",
"locDesc": "someLocDesc",
"ase": 1.00000000000000000,
"aseStartDate": "2023-07-01",
"indDate": "2023-12-30"
}
],
"message": "",
"warning": "",
"responseCode": "0"
}
答:
0赞
hermit
8/16/2023
#1
如果使用 Jackson 进行序列化,则放置类将始终包含所有类属性,无论其值如何。因此,也包括 null 值。JsonInclude(JsonInclude.Include.ALWAYS)
SearchResponse
评论
0赞
wheelerlc64
8/16/2023
奏效了!谢谢你的建议。我已将其标记为答案,因为它与杰克逊有关,杰克逊用于序列化数据,并且也用于简化非常有用的解决方案。
0赞
hermit
8/16/2023
很高兴它有帮助。
评论
SearchResponse
java.util.Date
Date
LocalDate