当前位置: 首页 > 知识库问答 >
问题:

Springframework数据弹性搜索无法创建java.time.ZonedDateTime类的对象

司徒正信
2023-03-14

我正在使用推荐的 RestHightCLient 和 ElasticsearchRestTemplate。我正在尝试从弹性搜索中检索ZonedDateTime,但似乎spring-data-elasticsearch无法创建ZonedDateTime类的对象。下面的代码片段有任何问题,我需要添加哪些其他配置。

此外,如果我要直接使用ResHighLevel客户端,而不使用Elasticsearch ch的操作,我需要做什么。

    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("content-type", "application/json");
        ClientConfiguration clientConfiguration = ClientConfiguration
                .builder()
                .connectedTo("localhost:9200")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }

    @Bean(name = {"elasticsearchTemplate"})
    public ElasticsearchOperations elasticsearhTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.elasticsearch.core.convert.ConversionException: could not create object of class java.time.ZonedDateTime] with root cause

java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2020, DayOfMonth=25, OffsetSeconds=3600, MonthOfYear=8},ISO resolved to 16:46:47.891 of type java.time.format.Parsed
    at java.base/java.time.LocalDate.from(LocalDate.java:397) ~[na:na]
public class XXClass {
    ....

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS Z")
    private ZonedDateTime created = ZonedDateTime.now();
}

共有1个答案

盖玉石
2023-03-14

请更改您的模式,使用uuuu而不是yyyy这里记录了Elasticsearch对此负责的更改:https://www . elastic . co/guide/en/elastic search/reference/current/migrate-to-Java-time . html # Java-time-migration-incompatible-date-formats

顺便说一句,你不需要

httpHeaders.add("content-type", "application/json");

Spring Data Elasticsearch不需要@Json…注释。

 类似资料:
  • 我在使用Python弹性搜索访问数据时遇到了一个问题。我正在得到 尝试使用时出错 我的弹性搜索版本6.5。4,python版本3.7。2.

  • 我有一个问题,在elasticsearch与mongob建立河流。如果日期的大小在一百万以内,我可以从mongob导入数据。但是当数据很大1000万或更大时,河流无法索引来自mongob集合的所有记录。 我在日志中看到这个错误 通常说river stale是错误的几次。此外,我在mongodb设置中的oplog大小为1024MB。

  • 升级到Spring boot 2.3和Spring data elasticsearch 4.0.9后的问题。我有这样的文档: 这在spring data 3.0中与Jackson配合得很好,但升级到4.0后,Jackson不再可用,现在我收到了一个来自spring的实例化异常,无法实例化URL对象。 例外情况: 任何关于解决方案的想法都将受到赞赏。

  • 是否缺少配置设置?

  • 我使用Spring boot 2.0.1。RELEASE/Spring数据Elasticsearch 3.0.6。我用@Document annotation注释我的域类,我有一个字段如下: 如您所见,我需要将字段类型设置为IP_范围(存在于弹性搜索引擎数据类型中),但不存在于FieldType enum中。 我想创建这个文件索引通过Elasticsearch chTemplate.create索

  • 我试图使用docker容器创建一个弹性搜索安装。我只使用Elastic.io提供者的映像。 我不知道为什么,但logstash告诉我,他无法连接到带有此错误消息的ElasticSearch实例: 如果logstash真的得到了我的设置,有人能告诉我为什么他使用了一个坏的主机事件吗?