Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Elasticsearch检索ZoneDateTime-未能将源[{}]映射到类_Java_Spring Boot_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Spring Data_Zoneddatetime - Fatal编程技术网 elasticsearch,spring-data,zoneddatetime,Java,Spring Boot,elasticsearch,Spring Data,Zoneddatetime" /> elasticsearch,spring-data,zoneddatetime,Java,Spring Boot,elasticsearch,Spring Data,Zoneddatetime" />

Java Elasticsearch检索ZoneDateTime-未能将源[{}]映射到类

Java Elasticsearch检索ZoneDateTime-未能将源[{}]映射到类,java,spring-boot,elasticsearch,spring-data,zoneddatetime,Java,Spring Boot,elasticsearch,Spring Data,Zoneddatetime,我使用SpringDataElasticSearch存储库存储数据,并使用高级rest客户端从Elasticsearch检索数据。存储的字段类型为ZonedDateTime,虽然它成功地存储了值,但在将其映射回数据类时出错 尝试按id检索文档时引发此错误: org.springframework.data.elasticsearch.ElasticsearchException: failed to map source [ {"time":"2020-04-20T14:01:38.470305

我使用SpringDataElasticSearch存储库存储数据,并使用高级rest客户端从Elasticsearch检索数据。存储的字段类型为ZonedDateTime,虽然它成功地存储了值,但在将其映射回数据类时出错

尝试按id检索文档时引发此错误:

org.springframework.data.elasticsearch.ElasticsearchException: failed to map source [ {"time":"2020-04-20T14:01:38.470305Z"}] to class TimeTest] with root cause
timeTest数据类中的字段(保存该字段)及其扩展的测试类(保存id):

customEntityConfiguration,以便Jackson识别Java日期和时间API

 @Bean
  ElasticsearchRestTemplate elasticsearchTemplate(ObjectMapper objectMapper) {
    return new ElasticsearchRestTemplate(client(), new CustomEntityMapper(objectMapper));
  }

  public class CustomEntityMapper implements EntityMapper {

    private final ObjectMapper objectMapper;

    public CustomEntityMapper(ObjectMapper objectMapper) {
      this.objectMapper = objectMapper;
      objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
      objectMapper.registerModule(new CustomGeoModule());
      objectMapper.registerModule(new JavaTimeModule());
    }

    @Override
    public String mapToString(final Object object) throws IOException {
      return objectMapper.writeValueAsString(object);
    }

    @Override
    public <T> T mapToObject(final String source, final Class<T> clazz) throws IOException {
      return objectMapper.readValue(source, clazz);
    }

    @Override
    public Map<String, Object> mapObject(Object source) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public <T> T readObject(Map<String, Object> source, Class<T> targetType) {
      // TODO Auto-generated method stub
      return null;
    }

  }
从curl localhost:9200/test/_doc/12345?pretty的Json响应中可以看出,字段存储正确

{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "12345",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "time" : "2020-04-20T14:01:38.470305Z"
  }
}
  @Override
  public void store(ZonedDateTime time) {
    repository.save(time);
  }

  @Override
  public TimeTest getResult(String id) {
    return repository.findById(id);
  }
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "12345",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "time" : "2020-04-20T14:01:38.470305Z"
  }
}