gpt4 book ai didi

java - 如何在 lamda 流的反序列化过程中忽略 NULL 值?

转载 作者:行者123 更新时间:2023-11-30 10:21:08 28 4
gpt4 key购买 nike

我有getter方法

@JsonInclude(Include.NON_NULL)
public Date getVerifiedFrom() {
if(invoices == null || invoices.isEmpty()) {
return null;
}
return invoices.stream().filter(i->i.getVerifiedDate() != null).map(Invoice::getVerifiedDate).min(Date::compareTo).get();
}

我尝试了几个链接,但没有帮助,

http://www.java2novice.com/java-json/jackson/ignore-json-null-elements/

How to deserialize Jackson Json NULL String to Date with JsonFormat

http://www.davismol.net/2016/01/08/jackson-how-to-exclude-null-value-properties-from-json-serialization/

How to tell Jackson to ignore a field during serialization if its value is null?

错误:

Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No value present; 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No value present (through reference chain: java.util.ArrayList[0]->com.enkindle.service.resource.TradeDebtResource["verifiedFrom"])

最佳答案

你得到的异常是由于流是空的,这导致 min 返回一个空的可选值,当你调用 get 时会抛出异常。您没有得到原始 NPE 的原因是 Jackson 可能只提取了消息。

您应该将 get() 替换为 orElse(null)。还有一个处理 Optional 的 Jackson 模块。

关于java - 如何在 lamda 流的反序列化过程中忽略 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967980/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com