如何在lambda流中反序列化期间忽略NULL值?

问题描述 投票:2回答:1

我有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?

错误:

解决了由Handler执行引起的异常:org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:没有值存在;嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:没有值存在(通过引用链:java.util.ArrayList [0] - > com.enkindle.service.resource.TradeDebtResource [“verifiedFrom”])

java spring spring-boot jackson2
1个回答
1
投票

您获得的异常是由于流为空,这导致min返回一个空的可选项,当您调用get时会抛出异常。你没有获得原始NPE的原因是杰克逊可能只提取信息。

你应该用get()替换orElse(null)。还有一个Jackson模块可以处理Optional

© www.soinside.com 2019 - 2024. All rights reserved.