diff --git a/src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java b/src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java
index b33319b6c..e7d35eaa6 100644
--- a/src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java
+++ b/src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java
@@ -19,6 +19,8 @@ import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
+import org.springframework.data.elasticsearch.core.ResultsMapper;
+import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
/**
* @author Christoph Strobl
@@ -42,8 +44,9 @@ public abstract class AbstractElasticsearchConfiguration extends ElasticsearchCo
*
* @return never {@literal null}.
*/
- @Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
- public ElasticsearchOperations elasticsearchOperations() {
- return new ElasticsearchRestTemplate(elasticsearchClient(), elasticsearchConverter(), resultsMapper());
+ @Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
+ public ElasticsearchOperations elasticsearchOperations(MappingElasticsearchConverter mappingElasticsearchConverter,
+ ResultsMapper resultsMapper) {
+ return new ElasticsearchRestTemplate(elasticsearchClient(), mappingElasticsearchConverter, resultsMapper);
}
}
diff --git a/src/main/java/org/springframework/data/elasticsearch/config/AbstractReactiveElasticsearchConfiguration.java b/src/main/java/org/springframework/data/elasticsearch/config/AbstractReactiveElasticsearchConfiguration.java
index 7f26f1967..f74b4be62 100644
--- a/src/main/java/org/springframework/data/elasticsearch/config/AbstractReactiveElasticsearchConfiguration.java
+++ b/src/main/java/org/springframework/data/elasticsearch/config/AbstractReactiveElasticsearchConfiguration.java
@@ -22,6 +22,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
+import org.springframework.data.elasticsearch.core.ResultsMapper;
+import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.lang.Nullable;
/**
@@ -47,10 +49,11 @@ public abstract class AbstractReactiveElasticsearchConfiguration extends Elastic
* @return never {@literal null}.
*/
@Bean
- public ReactiveElasticsearchOperations reactiveElasticsearchTemplate() {
+ public ReactiveElasticsearchOperations reactiveElasticsearchTemplate(
+ MappingElasticsearchConverter mappingElasticsearchConverter, ResultsMapper resultsMapper) {
ReactiveElasticsearchTemplate template = new ReactiveElasticsearchTemplate(reactiveElasticsearchClient(),
- elasticsearchConverter(), resultsMapper());
+ mappingElasticsearchConverter, resultsMapper);
template.setIndicesOptions(indicesOptions());
template.setRefreshPolicy(refreshPolicy());
diff --git a/src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java b/src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java
index 11916598f..3c4636aec 100644
--- a/src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java
+++ b/src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchConfigurationSupport.java
@@ -27,14 +27,13 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
+import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.elasticsearch.annotations.Document;
-import org.springframework.data.elasticsearch.core.DefaultEntityMapper;
import org.springframework.data.elasticsearch.core.DefaultResultMapper;
import org.springframework.data.elasticsearch.core.EntityMapper;
import org.springframework.data.elasticsearch.core.ResultsMapper;
-import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
@@ -43,14 +42,16 @@ import org.springframework.util.StringUtils;
/**
* @author Christoph Strobl
+ * @author Peter-Josef Meisch
* @since 3.2
*/
@Configuration
public class ElasticsearchConfigurationSupport {
@Bean
- public ElasticsearchConverter elasticsearchConverter() {
- return new MappingElasticsearchConverter(elasticsearchMappingContext());
+ public MappingElasticsearchConverter elasticsearchEntityMapper(
+ SimpleElasticsearchMappingContext elasticsearchMappingContext) {
+ return new MappingElasticsearchConverter(elasticsearchMappingContext);
}
/**
@@ -71,33 +72,15 @@ public class ElasticsearchConfigurationSupport {
return mappingContext;
}
- /**
- * Returns the {@link EntityMapper} used for mapping between the source and domain type.
- * Hint: you can use {@link org.springframework.data.elasticsearch.core.ElasticsearchEntityMapper} as
- * an alternative to the {@link DefaultEntityMapper}.
- *
- *
- * ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(elasticsearchMappingContext(),
- * new DefaultConversionService());
- * entityMapper.setConversions(elasticsearchCustomConversions());
- *
- *
- * @return never {@literal null}.
- */
- @Bean
- public EntityMapper entityMapper() {
- return new DefaultEntityMapper(elasticsearchMappingContext());
- }
-
/**
* Returns the {@link ResultsMapper} to be used for search responses.
*
- * @see #entityMapper()
+ * @see MappingElasticsearchConverter
* @return never {@literal null}.
*/
@Bean
- public ResultsMapper resultsMapper() {
- return new DefaultResultMapper(elasticsearchMappingContext(), entityMapper());
+ public ResultsMapper resultsMapper(SimpleElasticsearchMappingContext elasticsearchMappingContext) {
+ return new DefaultResultMapper(elasticsearchMappingContext, elasticsearchEntityMapper(elasticsearchMappingContext));
}
/**
diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java
deleted file mode 100644
index 1806c59e5..000000000
--- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * Copyright 2014-2019 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.springframework.data.elasticsearch.core;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-
-import org.springframework.data.annotation.ReadOnlyProperty;
-import org.springframework.data.elasticsearch.Document;
-import org.springframework.data.elasticsearch.core.geo.CustomGeoModule;
-import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
-import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
-import org.springframework.data.mapping.MappingException;
-import org.springframework.data.mapping.context.MappingContext;
-import org.springframework.util.Assert;
-
-import com.fasterxml.jackson.databind.BeanDescription;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationConfig;
-import com.fasterxml.jackson.databind.module.SimpleModule;
-import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
-import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
-
-/**
- * EntityMapper based on a Jackson {@link ObjectMapper}.
- *
- * @author Artur Konczak
- * @author Petar Tahchiev
- * @author Oliver Gierke
- * @author Christoph Strobl
- * @author Mark Paluch
- */
-public class DefaultEntityMapper implements EntityMapper {
-
- private ObjectMapper objectMapper;
-
- /**
- * Creates a new {@link DefaultEntityMapper} using the given {@link MappingContext}.
- *
- * @param context must not be {@literal null}.
- */
- public DefaultEntityMapper(
- MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> context) {
-
- Assert.notNull(context, "MappingContext must not be null!");
-
- objectMapper = new ObjectMapper();
-
- objectMapper.registerModule(new SpringDataElasticsearchModule(context));
- objectMapper.registerModule(new CustomGeoModule());
-
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
- }
-
- /*
- * (non-Javadoc)
- * @see org.springframework.data.elasticsearch.core.EntityMapper#mapToString(java.lang.Object)
- */
- @Override
- public String mapToString(Object object) throws IOException {
- return objectMapper
- .writeValueAsString(object instanceof Document ? new LinkedHashMap<>((Document) object) : object);
- }
-
- /*
- * (non-Javadoc)
- * @see org.springframework.data.elasticsearch.core.EntityMapper#mapObject(java.lang.Object)
- */
- @Override
- public Document mapObject(Object source) {
-
- try {
- return objectMapper.readValue(mapToString(source), Document.class);
- } catch (IOException e) {
- throw new MappingException(e.getMessage(), e);
- }
- }
-
- /*
- * (non-Javadoc)
- * @see org.springframework.data.elasticsearch.core.EntityMapper#mapToObject(java.lang.String, java.lang.Class)
- */
- @Override
- public T mapToObject(String source, Class clazz) throws IOException {
- return objectMapper.readValue(source, clazz);
- }
-
- /*
- * (non-Javadoc)
- * @see org.springframework.data.elasticsearch.core.EntityMapper#readObject(java.util.Map, java.lang.Class)
- */
- @Override
- public T readObject(Document source, Class targetType) {
- try {
- return mapToObject(mapToString(source), targetType);
- } catch (IOException e) {
- throw new MappingException(e.getMessage(), e);
- }
- }
-
- /**
- * A simple Jackson module to register the {@link SpringDataSerializerModifier}.
- *
- * @author Oliver Gierke
- * @since 3.1
- */
- private static class SpringDataElasticsearchModule extends SimpleModule {
-
- private static final long serialVersionUID = -9168968092458058966L;
-
- /**
- * Creates a new {@link SpringDataElasticsearchModule} using the given {@link MappingContext}.
- *
- * @param context must not be {@literal null}.
- */
- public SpringDataElasticsearchModule(
- MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> context) {
-
- Assert.notNull(context, "MappingContext must not be null!");
-
- setSerializerModifier(new SpringDataSerializerModifier(context));
- }
-
- /**
- * A {@link BeanSerializerModifier} that will drop properties annotated with {@link ReadOnlyProperty} for
- * serialization.
- *
- * @author Oliver Gierke
- * @since 3.1
- */
- private static class SpringDataSerializerModifier extends BeanSerializerModifier {
-
- private final MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> context;
-
- public SpringDataSerializerModifier(
- MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> context) {
-
- Assert.notNull(context, "MappingContext must not be null!");
-
- this.context = context;
- }
-
- /*
- * (non-Javadoc)
- * @see com.fasterxml.jackson.databind.ser.BeanSerializerModifier#changeProperties(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.BeanDescription, java.util.List)
- */
- @Override
- public List changeProperties(SerializationConfig config, BeanDescription description,
- List properties) {
-
- Class> type = description.getBeanClass();
- ElasticsearchPersistentEntity> entity = context.getPersistentEntity(type);
-
- if (entity == null) {
- return super.changeProperties(config, description, properties);
- }
-
- List result = new ArrayList<>(properties.size());
-
- for (BeanPropertyWriter beanPropertyWriter : properties) {
-
- ElasticsearchPersistentProperty property = entity.getPersistentProperty(beanPropertyWriter.getName());
-
- if (property != null && property.isWritable()) {
- result.add(beanPropertyWriter);
- }
- }
-
- return result;
- }
- }
- }
-}
diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java
index 576812924..41c94c474 100644
--- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java
+++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java
@@ -32,6 +32,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
+import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
@@ -65,7 +66,7 @@ public class DefaultResultMapper extends AbstractResultMapper {
public DefaultResultMapper(
MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> mappingContext) {
- this(mappingContext, initEntityMapper(mappingContext));
+ this(mappingContext, null);
}
public DefaultResultMapper(EntityMapper entityMapper) {
@@ -84,7 +85,9 @@ public class DefaultResultMapper extends AbstractResultMapper {
MappingContext extends ElasticsearchPersistentEntity>, ElasticsearchPersistentProperty> mappingContext) {
Assert.notNull(mappingContext, "MappingContext must not be null!");
- return new DefaultEntityMapper(mappingContext);
+ MappingElasticsearchConverter mappingElasticsearchConverter = new MappingElasticsearchConverter(mappingContext, null);
+ mappingElasticsearchConverter.afterPropertiesSet();
+ return mappingElasticsearchConverter;
}
@Override
diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchEntityMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchEntityMapper.java
deleted file mode 100644
index 924dd6adb..000000000
--- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchEntityMapper.java
+++ /dev/null
@@ -1,732 +0,0 @@
-/*
- * Copyright 2019 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.springframework.data.elasticsearch.core;
-
-import lombok.RequiredArgsConstructor;
-
-import java.io.IOException;
-import java.util.*;
-import java.util.Map.Entry;
-
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.core.CollectionFactory;
-import org.springframework.core.convert.ConversionService;
-import org.springframework.core.convert.support.DefaultConversionService;
-import org.springframework.core.convert.support.GenericConversionService;
-import org.springframework.data.convert.CustomConversions;
-import org.springframework.data.convert.EntityConverter;
-import org.springframework.data.convert.EntityInstantiator;
-import org.springframework.data.convert.EntityInstantiators;
-import org.springframework.data.convert.EntityReader;
-import org.springframework.data.convert.EntityWriter;
-import org.springframework.data.elasticsearch.Document;
-import org.springframework.data.elasticsearch.SearchDocument;
-import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
-import org.springframework.data.elasticsearch.core.convert.ElasticsearchTypeMapper;
-import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
-import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
-import org.springframework.data.mapping.PersistentPropertyAccessor;
-import org.springframework.data.mapping.context.MappingContext;
-import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
-import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
-import org.springframework.data.mapping.model.PropertyValueProvider;
-import org.springframework.data.util.ClassTypeInformation;
-import org.springframework.data.util.Streamable;
-import org.springframework.data.util.TypeInformation;
-import org.springframework.format.datetime.DateFormatterRegistrar;
-import org.springframework.lang.Nullable;
-import org.springframework.util.ClassUtils;
-import org.springframework.util.ObjectUtils;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.ObjectReader;
-import com.fasterxml.jackson.databind.ObjectWriter;
-
-/**
- * Elasticsearch specific {@link EntityReader} & {@link EntityWriter} implementation based on domain type
- * {@link ElasticsearchPersistentEntity metadata}.
- *
- * @author Christoph Strobl
- * @author Peter-Josef Meisch
- * @author Mark Paluch
- * @since 3.2
- */
-public class ElasticsearchEntityMapper
- implements EntityConverter, ElasticsearchPersistentProperty, Object, Document>,
- EntityWriter