gpt4 book ai didi

java - 在 Spring Boot 和 ElasticSearch 中使用 Instant、LocalDateTime 和 ZonedDateTime

转载 作者:行者123 更新时间:2023-11-29 04:09:23 29 4
gpt4 key购买 nike

我正在使用 Spring Boot 2.1.4 和 Spring Data Jest与 Elasticsearch 。我最初将 Java Date 用于某些带有以下注释的属性:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")

这被保存到 ElasticSearch 中,如下所示:

"creationDate": "2019-04-10T14:49:05.672+0000"

现在,我正在从 Date 迁移到 LocalDateTime 和 ZonedDateTime。当数据现在保存到 ElasticSearch 时,我保存了以下属性:

"creationDate": {
"dayOfYear": 123,
"dayOfWeek": "FRIDAY",
"month": "MAY",
"dayOfMonth": 3,
"year": 2019,
"monthValue": 5,
"hour": 11,
"minute": 54,
"second": 12,
"nano": 238000000,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},

我需要做什么来更改它,以便我获得与 LocalDateTime 和 ZonedDateTime 之前相同的 ElasticSearch 数据格式?

我尝试了以下方法:

  1. 自定义对象映射器如下:

    public class CustomEntityMapper implements EntityMapper {
    private final ObjectMapper objectMapper;

    public CustomEntityMapper(ObjectMapper objectMapper) {
    this.objectMapper = new 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(Object object) throws IOException {
    return objectMapper.writeValueAsString(object);
    }

    @Override
    public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
    return objectMapper.readValue(source, clazz);
    }
    }
  2. 将以下内容添加到对象映射器:

    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

如有任何帮助或指出我出错的地方,我们将不胜感激。

最佳答案

设法让它与 Spring Boot 2.1.4 和 Spring Data Jest 一起工作。这是我所做的:

  1. 示例域对象:

    @Document(indexName = "datetest")
    public class DateTest {

    @Id
    private String id;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ", timezone = "UTC")
    private Instant instant = Instant.now();

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
    private ZonedDateTime zonedDateTime = ZonedDateTime.now();

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSS")
    private LocalDateTime localDateTime = LocalDateTime.now();

    // getters/setters
    }
  2. ElasticSearch/JEST 配置:

    @Configuration
    public class ESConfig {

    @Bean
    public EntityMapper getEntityMapper() {
    return new CustomEntityMapper();
    }

    @Bean
    @Primary
    public ElasticsearchOperations elasticsearchTemplate(final JestClient jestClient,
    final ElasticsearchConverter elasticsearchConverter,
    final SimpleElasticsearchMappingContext simpleElasticsearchMappingContext, EntityMapper mapper) {
    return new JestElasticsearchTemplate(jestClient, elasticsearchConverter,
    new DefaultJestResultsMapper(simpleElasticsearchMappingContext, mapper));
    }

    public class CustomEntityMapper implements EntityMapper {

    private final ObjectMapper objectMapper;

    public CustomEntityMapper() {
    objectMapper = new 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(Object object) throws IOException {
    return objectMapper.writeValueAsString(object);
    }

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

    }
    }
  3. ElasticSearch 中的结果:

    Screenshot of results in ElasticSearch

希望这对您有所帮助。

关于java - 在 Spring Boot 和 ElasticSearch 中使用 Instant、LocalDateTime 和 ZonedDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007340/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com