如何在 lamda 流的反序列化过程中忽略 NULL 值?
How to ignore a NULL value during Deserialization in lamda stream?
我有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();
}
我尝试了几次 link 但这些都没有帮助,
http://www.java2novice.com/java-json/jackson/ignore-json-null-elements/
How to deserialize Jackson Json NULL String to Date with JsonFormat
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
到 return 一个空的可选,当你调用 get
时抛出异常。
你没有得到原始 NPE 的原因是 Jackson 可能只提取了消息。
您应该将 get()
替换为 orElse(null)
。
还有一个处理 Optional
.
的 Jackson 模块
我有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();
}
我尝试了几次 link 但这些都没有帮助,
http://www.java2novice.com/java-json/jackson/ignore-json-null-elements/
How to deserialize Jackson Json NULL String to Date with JsonFormat
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
到 return 一个空的可选,当你调用 get
时抛出异常。
你没有得到原始 NPE 的原因是 Jackson 可能只提取了消息。
您应该将 get()
替换为 orElse(null)
。
还有一个处理 Optional
.