spring-boot - 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)
解决方案
与答案没有直接关系:这是包中的@LastModifiedDate
注释吗?org.springframework.data.annotation
您是否在使用 Spring Data Elasticsearch Auditing(请参阅文档)?您不需要自己设置字段的值。如果您不使用审核,则可以删除注释。
对于您的问题:格式DateFormat.basic_date_time
是带有时间和时区的日期。但是 aLocalDateTime
没有时区。您有两种选择:
- 对于我会选择的审计字段,使用支持时区的 Java 数据类型
Instant
。 - 使用不需要时区的 Elasticsearch 日期格式,例如
DateFormat.date_hour_minute_second_millis
. 如果您更改它,您需要使用映射重新创建索引。
我个人会选择第一个。
推荐阅读
- wiremock - 根据查询参数动态返回文件
- angular - 子路由重定向到主路由
- artifactory - 如何使用 JFrog Artifactory 打包或捆绑工件以进行配置管理?
- python - 如何从 Python 3 中的函数导入变量
- python - 在 Python 中对文件中的分数进行排序
- ios - iOS 模拟器版本的 iPhone 未显示带有错误的 console.log 输出
- javascript - Redux 操作:返回带有键但没有值的对象的含义?
- powershell - 递归文件重命名行为不正确
- python - 将参数传递给 scipy.stats.binned_statistic 的用户函数
- java - 如何从 Node.java 文件访问增强图像的 getIndex