Java在ElasticSearch中使用LocalDatetime類型
遇到的問題
最近在開發(fā)一個搜索功能的需求的時候,遇到了LocalDatetime類型不能保存到ElasticSearch中的問題,報錯如下:
ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=failed to parse field [createTime] of type [date] in document with id '3000'] ]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_state_exception, reason=Can't get text on a START_OBJECT at 1:125]];
從網(wǎng)上查找嘗試第一個辦法:
將以下注解加到時間字段上,依然無效
@Field(type = FieldType.Date, index = FieldIndex.not_analyzed, store = true,
format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd HH:mm:ss.SSS")
private LocalDateTime createTime;解決辦法
在項目中添加以下依賴:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>在字段上加上以下注解
@JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) @Field(type = FieldType.Date, store = true, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss.SSS") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS") private LocalDateTime createTime;
完美解決
到此這篇關于Java在ElasticSearch中使用LocalDatetime類型的文章就介紹到這了,更多相關Java在ElasticSearch中使用LocalDatetime類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java利用opencv實現(xiàn)用字符展示視頻或圖片的方法
這篇文章主要介紹了Java利用opencv實現(xiàn)用字符展示視頻或圖片的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
maven導入本地倉庫jar包,報:Could?not?find?artifact的解決
這篇文章主要介紹了maven導入本地倉庫jar包,報:Could?not?find?artifact的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
springboot實現(xiàn)以代碼的方式配置sharding-jdbc水平分表
這篇文章主要介紹了springboot實現(xiàn)以代碼的方式配置sharding-jdbc水平分表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot監(jiān)控API請求耗時的6中解決解決方案
本文介紹SpringBoot中記錄API請求耗時的6種方案,包括手動埋點、AOP切面、攔截器、Filter、事件監(jiān)聽、Micrometer+Prometheus,具有一定的參考價值,感興趣的可以了解一下2025-07-07
Springboot項目編譯后未能加載靜態(tài)資源文件的問題
這篇文章主要介紹了Springboot項目編譯后未能加載靜態(tài)資源文件的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

