Skip to content

Commit

Permalink
Elasticsearch: custom converters for java.time classes
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed May 2, 2020
1 parent 3ca37ca commit 329190c
Showing 1 changed file with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
-%>
package <%= packageName %>.config;

<%_ if (databaseType === 'mongodb') { _%>
import org.springframework.context.annotation.Bean;
<%_ } _%>
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
<%_ if (databaseType === 'mongodb') { _%>
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
Expand All @@ -32,8 +35,95 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
<%_ } _%>

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;

@Configuration
public class ElasticsearchConfiguration {
public class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {

@Bean
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(
new ZonedDateTimeWritingConverter(),
new ZonedDateTimeReadingConverter(),
new InstantWritingConverter(),
new InstantReadingConverter(),
new LocalDateWritingConverter(),
new LocalDateReadingConverter()
)
);
}

@WritingConverter
static class ZonedDateTimeWritingConverter implements Converter<ZonedDateTime, String> {
@Override
public String convert(ZonedDateTime source) {
if (source == null) {
return null;
}
return source.toInstant().toString();
}
}

@ReadingConverter
static class ZonedDateTimeReadingConverter implements Converter<String, ZonedDateTime> {
@Override
public ZonedDateTime convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source).atZone(ZoneId.systemDefault());
}
}

@WritingConverter
static class InstantWritingConverter implements Converter<Instant, String> {
@Override
public String convert(Instant source) {
if (source == null) {
return null;
}
return source.toString();
}
}

@ReadingConverter
static class InstantReadingConverter implements Converter<String, Instant> {
@Override
public Instant convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source);
}
}

@WritingConverter
static class LocalDateWritingConverter implements Converter<LocalDate, String> {
@Override
public String convert(LocalDate source) {
if (source == null) {
return null;
}
return source.toString();
}
}

@ReadingConverter
static class LocalDateReadingConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {
if (source == null) {
return null;
}
return LocalDate.parse(source);
}
}
<%_ if (databaseType === 'mongodb') { _%>

@Bean
Expand Down

0 comments on commit 329190c

Please sign in to comment.