我目前正在处理一个场景,在数据库中我有一个设置了 NULL 值的日期列。当我测试下面的代码并获得响应时,当该值为 NULL 时,该字段甚至不会出现在我的响应中。如何自动发送响应中的字段并处理 NULL 场景,发送空值(例如空字符串)?该字段需要在响应中发送(userIndDate,它是一个 java.util.Date 对象)。下面是代码片段?
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"
}
如果您使用 Jackson 进行序列化,则将
JsonInclude(JsonInclude.Include.ALWAYS)
放在 SearchResponse
类上将始终包含所有类属性,无论其值如何。