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

标签 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?

错误:

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/

相关文章:

java - 在 Play Framework 2.2.x 中设置代理选项时 WS.url 失败

java - 如何在 Eclipse 中使用 Google Drive Api?

java - Spring:如何将@RestClientTest 与带有@Qualifier 的RestTemplate 结合起来?

spring - nginx 'interfering' 缓存响应?

java - API测试+事务回滚

java - 使用 Maven 的 Spring Boot 示例不会在代理后面编译

Java BufferedImage 增加宽度

java - Android 在内部存储上创建文件,我做错了什么?

java - Json Deserializer - 如何从父 JSON 对象获取数据

ios - 使用 spring boot 在 mac 中保存文件