-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Custom ObjectMapper
Peter-Josef Meisch edited this page Jan 2, 2021
·
3 revisions
ObjectMapper was removed in Spring Data Elasticsearch 4.0!
|
---|
How to use custom ObjectMapper?
- Create new Class and implement Interface EntityMapper
public class CustomEntityMapper implements EntityMapper {
public CustomEntityMapper(){
//custom configuration/implementation (e.g. FasterXML/jackson)
}
@Override
public String mapToString(Object object) throws IOException {
//mapping Object to String
return null;
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
//mapping String to Object
return null;
}
}
- Define new bean and inject it into ElasticsearchTemplate
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
<constructor-arg name="entityMapper" ref="entityMapper"/>
</bean>
<bean name="entityMapper" class="org.springframework.data.elasticsearch.core.CustomEntityMapper"/>
- Inject your custom implementation to template
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<elasticsearch:repositories base-package="com.xyz.acme"/>
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
<constructor-arg name="resultsMapper" ref="resultMapper"/>
</bean>
<bean name="entityMapper" class="org.springframework.data.elasticsearch.core.DefaultEntityMapper"/>
<bean name="resultMapper" class="org.springframework.data.elasticsearch.core.CustomResultMapper">
<constructor-arg ref="entityMapper"/>
</bean>
</beans>
Default implementation for EntityMapper used in project can be found here https://github.com/spring-projects/spring-data-elasticsearch/blob/master/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java