spring-data-elasticsearch - Jackson 无法使用全局配置进行序列化

spring-data-elasticsearch - Jackson can't serialize using global configuration

我正在使用 ElasticSearch 开发一个项目,但我在 serialization/deserialization 和 Jackson 之间遇到了一些问题。我的项目是使用 JHipster 创建的,因此,我使用 spring 将我的实体存储到数据库并在 ElasticSearch 中建立索引。所有实体和其他对象都可以使用 Jackson 进行(反)序列化,除非我尝试将其添加到 ES。 这是我对 Jackson 的全局配置:

@Configuration
public class JacksonConfiguration {

    @Bean
    Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        SimpleModule timeModule = new JavaTimeModule();
        timeModule.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        timeModule.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        timeModule.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        timeModule.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
        timeModule.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
        SimpleModule geoModule=new GeoModule();
        geoModule.addSerializer(Point.class, PointSerializer.INSTANCE);
        geoModule.addDeserializer(Point.class, PointDeserializer.INSTANCE);
        return new Jackson2ObjectMapperBuilder()
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .findModulesViaServiceLoader(true)
            .modulesToInstall(timeModule,geoModule);
    }
}

此配置工作正常,除非我尝试将实体添加到 ES,例如,从未调用 PointSerializer。我可以看到这个 ES 的序列化器 运行(并因此正确索引)的唯一方法是将 @JsonSerialize(using = PointSerializer.class) 添加到字段中。为什么会发生这种情况,如何全局配置它?

似乎 Spring Data elasticsearch 没有为此使用默认的 spring Jackson2ObjectMapperBuilder。默认情况下使用此配置:

https://github.com/spring-projects/spring-data-elasticsearch/blob/master/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java

...您可以通过提供一些自定义对象映射器来覆盖它,如下所述:

https://github.com/spring-projects/spring-data-elasticsearch/wiki/Custom-ObjectMapper

在这里你当然可以直接使用你的Jackson ObjectMappers。有关更多详细信息,请在 jhipster github 仓库查看此问题:

https://github.com/jhipster/generator-jhipster/issues/2241#issuecomment-151933768