无法从TemporalAccessor获取即时消息:{},ISO解析为java.time.format.Parsed类型的2018-01-01

问题描述

我制作了具有以下映射的Elasticsearch(版本7.8.1)文档:

{
    "transaction": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text","fields": {
                        "keyword": {
                            "type": "keyword","ignore_above": 256
                        }
                    }
                
                },"settlementEntries": {
                    "type": "nested","properties": {
                        "settlementDate": {
                            "type" : "date","format" : "uuuu-MM-dd"
                        },"settlementId": {
                            "type": "long"
                        }
                    }
                },"transactionId": {
                    "type": "text","ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

我使用的格式为yyyy-MM-dd来存储沉降日期。使用curl进行查询时,可以使用CURL -X GET localhost:9200/transaction/_search查看数据。但是,当我尝试通过springboot进行相同操作时,会抛出错误。我的实体是:

public class TransactionBo {

    @Id
    private String transactionId;
    @Field(type = FieldType.nested)
    private SettlementEntryBo settlementEntries;
    
}

public class SettlementEntryBo {

    @Id
    private Long settlementId;
    @Nullable
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "uuuu-MM-dd")
private Date settlementDate;

}

据我所知,问题出在这里

@Field(type = FieldType.Date,pattern = "uuuu-MM-dd")
private Date settlementDate;

[错误片段]:

2020-09-05 02:51:18.105错误10080 --- [nio-8090-exec-4] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service()在路径为[]的情况下引发异常[请求处理失败;嵌套的异常为java.time.DateTimeException:无法从TemporalAccessor获取即时消息:{},ISO解析为类型为java.time.format.Parsed的2016年1月1日,其根本原因是

java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:InstantSeconds

解决方法

更改模式以使用 uuuu 代替 yyyy is documented here,Elasticsearch中的更改对此负责:https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

我看到的另一件事:

您使用的日期格式仅包含年,月和日。那是个简单的约会。但是java.util.Date不是日期,而是UTC区域中的即时时间-包括时间戳。

因此,您应该像Ole在其注释中建议的那样更改属性类型java.time.LocalDate。这些类是在Java 8中引入的,以克服java.util.Date所存在的所有缺陷。