jackson 在响应中将日期字段发送为空,而不是设置为空且不在响应中发送

u91tlkcl  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(76)

我目前正在处理一个场景,在DB中,我有一个日期列,它设置了NULL值。当我测试下面的代码并得到我的响应时,当该值为NULL时,该字段甚至没有出现在我的响应中。如何在响应中自动发送字段并处理NULL场景,发送空值(如空字符串)?在响应(userIndDate,它是一个java.util.Date对象)中发送时需要该字段。我正在使用Jackson序列化数据。我没有包括我的repo,它正在做db->objectMap,只是为了压缩代码。下面是代码片段?

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"
}
ktca8awb

ktca8awb1#

如果您使用Jackson进行序列化,则将JsonInclude(JsonInclude.Include.ALWAYS)放在SearchResponse类上将始终包含所有类属性,而不管它们的值如何。因此,也包括空值。

相关问题