首页 > 解决方案 > LastModifiedDate 应该使用哪些转换器,因为它会引发 UnsupportedTemporalTypeException 错误

问题描述

@Builder.Default
@LastModifiedDate
@Field(type = FieldType.Date, name = "mdt", format = DateFormat.basic_date_time)
private LocalDateTime modifiedDate = LocalDateTime.now();

21-06-08T19:10:25,367 TRACE [http-nio-5555-exec-1] c.p.m.e.GlobalExceptionHandler: Found generic exception
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
        at java.base/java.time.LocalDate.get0(LocalDate.java:709)
        at java.base/java.time.LocalDate.getLong(LocalDate.java:688)
        at java.base/java.time.LocalDateTime.getLong(LocalDateTime.java:721)
        at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
        at java.base/java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3569)
        at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2343)
        at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1847)
        at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1821)
        at org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter$PatternDateFormatter.format(ElasticsearchDateConverter.java:261)

标签: spring-bootelasticsearchspring-data-elasticsearch

解决方案


与答案没有直接关系:这是包中的@LastModifiedDate注释吗?org.springframework.data.annotation您是否在使用 Spring Data Elasticsearch Auditing(请参阅文档)?您不需要自己设置字段的值。如果您不使用审核,则可以删除注释。

对于您的问题:格式DateFormat.basic_date_time是带有时间时区的日期。但是 aLocalDateTime没有时区。您有两种选择:

  1. 对于我会选择的审计字段,使用支持时区的 Java 数据类型Instant
  2. 使用不需要时区的 Elasticsearch 日期格式,例如DateFormat.date_hour_minute_second_millis. 如果您更改它,您需要使用映射重新创建索引。

我个人会选择第一个。


推荐阅读