spring-data-elasticsearch与spring-boot-starter-data-elasticsearch的区别及报错解析
Spring Boot 2.7.15迁移后Elasticsearch保存报错及依赖区别解析
迁移至Spring Boot 2.7.15版本后,向Elasticsearch索引保存文档时触发如下错误:
2023-10-04 13:23:01 [pool-2-thread-10] ERROR o.s.s.s.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task java.lang.NoSuchFieldError: INDEX_CONTENT_TYPE at org.springframework.data.elasticsearch.core.RequestFactory.indexRequest(RequestFactory.java:771) at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.doIndex(ElasticsearchRestTemplate.java:156) at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.save(AbstractElasticsearchTemplate.java:189) at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.lambda$save$6(SimpleElasticsearchRepository.java:185) at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.executeAndRefresh(SimpleElasticsearchRepository.java:359) at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.save(SimpleElasticsearchRepository.java:185) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:530) at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:286) at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:640) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241) at jdk.proxy2/jdk.proxy2.$Proxy100.save(Unknown Source) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:241) at jdk.proxy2/jdk.proxy2.$Proxy100.save(Unknown Source)
切换为spring-boot-starter-data-elasticsearch依赖后问题解决,以下是这两个依赖的核心区别:
1. 版本兼容性管理
spring-data-elasticsearch:仅提供Elasticsearch数据操作的核心实现,不处理版本匹配,需手动确保其与Spring Boot、Elasticsearch客户端版本兼容。本次报错就是因为该依赖版本与Spring Boot 2.7.15不匹配,引用了新版Elasticsearch客户端已移除的INDEX_CONTENT_TYPE字段。spring-boot-starter-data-elasticsearch:Spring Boot官方启动器,自动继承Spring Boot的版本依赖管理,拉取与当前Boot版本完全兼容的Spring Data Elasticsearch、Elasticsearch客户端等组件,从根源避免版本冲突。
2. 依赖完整性
spring-data-elasticsearch:只包含核心数据操作类,需自行引入Elasticsearch REST客户端、连接池、Jackson序列化等配套依赖,易出现依赖缺失或版本不统一。spring-boot-starter-data-elasticsearch:一次性引入所有必要依赖,无需手动补充,开箱即可搭建Elasticsearch操作环境。
3. 自动配置支持
spring-data-elasticsearch:无Spring Boot自动配置能力,需手动编写配置类初始化RestHighLevelClient、ElasticsearchRestTemplate等核心Bean。spring-boot-starter-data-elasticsearch:提供自动配置,只需在配置文件中填写Elasticsearch地址、认证信息等参数,即可自动生成并注入所需的操作Bean,减少手动配置成本。
内容的提问来源于stack exchange,提问作者Athul Samuel
相关产品推荐
相关产品推荐

