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

时间:2017-12-25 11:00:45

标签: java spring spring-boot jackson2

我有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" ])

1 个答案:

答案 0 :(得分:1)

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

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