5

I am developing an application that uses spring-data-elasticsearch for the data storage. I am also writing this application using kotlin. This has some special quirks, one being that jackson needs the jackson-module-kotlin (to get rid of the problems with no args constructor). I also use the jackson-datatype-jsr310 module.

Spring boot is nice enough to pick these modules up from the classpath and register them automatically. I also have the write-dates-as-timestamps option set to false. However spring-data-elasticsearch seems to be oblivious to it. I figured that it is because the DefaultEntityMapper news an objectmapper and doesn't use the one from the spring context. I was wondering why? I fixed it by providing some extra configuration that makes sure that spring-data-elasticsearch uses the objectmapper that i get from the spring context:

@Configuration
open class ElasticsearchConfig(val elasticsearchEntityMapper: ElasticsearchEntityMapper) {

    @Bean
    open fun elasticsearchTemplate(client: Client) = ElasticsearchTemplate(client, DefaultResultMapper(elasticsearchEntityMapper))

}

@Component
class ElasticsearchEntityMapper(val objectMapper: ObjectMapper) : EntityMapper {
    override fun mapToString(`object`: Any?) = objectMapper.writeValueAsString(`object`)

    override fun <T : Any?> mapToObject(source: String?, clazz: Class<T>?) = objectMapper.readValue(source, clazz)

}

Is there a better/easier way to make sure spring-data-elasticsearch uses the objectmapper from the context?

Geert Olaerts
  • 1,155
  • 2
  • 9
  • 17

0 Answers0