Spring Data Elastic - Java.Time.Instant 类 Jackson deserliization 不起作用 [英] Spring Data Elastic - Java.Time.Instant class Jackson deserliization not working

查看:25
本文介绍了Spring Data Elastic - Java.Time.Instant 类 Jackson deserliization 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 WorkroomDTO:

Following is my WorkroomDTO:

  @NotNull
  private Instant createdOn;

  @JsonInclude(JsonInclude.Include.NON_NULL)
  private Instant changedOn;

如您所见,我使用的是 Java 8 Instant 类.在 elasticsearch 索引服务器中,我将以下内容存储为 JSON:

As you can see i am using Java 8 Instant class. In the elasticsearch Index server i store the following as JSON:

  "createdOn": {
    "nano": 877000000,
    "epochSecond": 1579861613
  },
  "changedOn": {
    "nano": 920000000,
    "epochSecond": 1579861613
  },

问题是当我查询 elasticsearch 服务器以获取工作室时

The problem is when i query the elasticsearch server to get me the workroom

    return elasticsearchOperations.queryForPage(new NativeSearchQueryBuilder().withQuery(mainQuery)
                                                                          .withPageable(elasticUtils.interceptPageable(searchDto.getPageable(), "name"))
                                                                          .build(),
                                            WorkroomDTO.class);

,我将这些字段映射到我的 WorkroomDTO 我得到以下异常:

, i make a mapping of these fields to my WorkroomDTO i get the following exception:

    com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.Instant` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"createdOn":{"nano":68000000,"epochSecond":1580127683}

仅供参考:

我创建了一个配置文件,其中将 JavaTimeModule 显式注册到对象映射器

I have created a configuration file where is register explicitly the JavaTimeModule to the Object Mapper

 @Configuration
public class JacksonConfiguration {

  @Value("${application.serialization.include-type-key:true}")
  private boolean includeTypeKey = true;

  @Bean
  public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.addHandler(new MissingTypeIdHandler());
    if (includeTypeKey) {
      mapper.setMixInResolver(new TypeKeyMixInResolver());
    }
    return mapper;
  }

}

需要帮助!

推荐答案

这些数据从何而来?它是由您的应用程序编写的吗?

Where does this data come from? Is it written by your application?

使用的 Jackson 映射器似乎没有注册 jackson-datatype-jsr310 模块.

It seems that the used Jackson mapper does not have the jackson-datatype-jsr310 module registered.

在读取数据时尝试找到可用于创建 Instant 对象的 Instant 构造函数.但是 Instant 没有默认构造函数,Mapper 应该使用 Instant.ofEpochSecond(long, long) 方法.此页面很好地说明了问题并展示了 Jackson Mapper 的配置方式.

On reading the data tries to find a constructor of Instant that can be used to create an Instant object. But Instant does not have a default constructor and the Mapper should use the Instant.ofEpochSecond(long, long) method. This page pretty declares the problem and shows how the Jackson Mapper is configured.

以这种方式存储瞬间,作为具有两个属性的对象,并不是在 Elasticsearch 中存储日期的正确方式.您应该阅读Elasticsearch 文档,了解 Elastcisearch 如何处理日期/时间字段.当将瞬间存储为这样的对象时,您将失去使用 Elasticsearch 查询和基于日期/时间的条件的能力.

Storing an instant in this way, as an object with two properties, is not the right way for storing dates in Elasticsearch. You should read the Elasticsearch documentation about how Elastcisearch handles date/time fields. When storing the instant as an object like this, you loose the ability to use Elasticsearch queries with criteria based on a date/time.

您使用哪个版本的 Spring Data Elasticsearch?由于这样的问题,从即将发布的 4.0 版本开始,Spring Data Elasticsearch 将不再使用 Jackson 映射器进行实体映射.MappingElasticsearchConverter 支持使用 Elasticsearch 日期格式和 java.time 类.

Which version of Spring Data Elasticsearch do you use? Because of problems like this, from the upcoming version 4.0 on, Spring Data Elasticsearch will not use the Jackson mapper anymore for entity mapping. The MappingElasticsearchConverter supports the use of the Elasticsearch date format and the java.time classes.

这篇关于Spring Data Elastic - Java.Time.Instant 类 Jackson deserliization 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆