【问题标题】:Spring Boot with Elastic Search causing java.lang.NoSuchFieldError: IGNORE_DEPRECATIONSSpring Boot 与 Elastic Search 导致 java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
【发布时间】:2020-10-01 22:26:47
【问题描述】:

我是弹性搜索的新手。开始使用 Elastic search 构建 Spring Boot 应用程序。

使用最新的 ES 版本 "elasticsearch-7.7.1" 并进行集成,我使用以下 maven 依赖项:

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.7.1</version>
    </dependency>

在我的 Spring Boot 应用中添加了以下配置:

@Configuration
public class ESConfig {

  @Bean(destroyMethod = "close")
  public RestHighLevelClient client() {
    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost")));
    return restHighLevelClient;
  }

}

将以下属性添加到 application.yaml

elasticsearch:
  host: localhost

在应用程序启动时遇到异常:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'client' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
    ... 19 common frames omitted
Caused by: java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
    at org.elasticsearch.client.RestHighLevelClient.<clinit>(RestHighLevelClient.java:1902)
    at com.sbs.communicationcontrol.search.config.ESConfig.client(ESConfig.java:14)

谁能帮忙解释一下为什么会出现这个异常?

【问题讨论】:

  • 检查您的弹性版本。必须是相同版本的驱动程序。是一样的吗?
  • @DevChauhan 已经有一段时间了,如果它对你有用,如果你能投票给答案,如果你需要更多信息,请告诉我

标签: java spring spring-boot maven elasticsearch


【解决方案1】:

经过一些研发,通过添加以下两个依赖项解决了这个问题:

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.7.1</version>
        </dependency>

【讨论】:

  • 这对我使用 spring-boot 2.1.5-RELEASE 和弹性搜索 7.9.3 有效。谢谢!
【解决方案2】:

我从6.8.5 升级到org.elasticsearch.client:elasticsearch-rest-high-level-client:7.8.1 时遇到了同样的问题。问题背后的原因是将 elasticsearch-rest-high-level-client 单独更新到最新版本与它的一些依赖弹性搜索依赖项冲突,这些依赖项是由 spring-boot 作为传递依赖项引入类路径的。当我查看依赖树时,发现org.springframework.boot:spring-boot:2.3.1.RELEASE依赖带来了org.elasticsearch.client:elasticsearch-rest-client:7.6.2org.elasticsearch:elasticsearch:7.6.2和7.6.2与7.8.1的冲突。

摘自 RestHighLevelClient 引用 IGNORE_DEPRECATIONS Java Docs 的代码 sn-p。

public class RestHighLevelClient implements Closeable {
    ....
    ....
    /**
     * Ignores deprecation warnings. This is appropriate because it is only
     * used to parse responses from Elasticsearch. Any deprecation warnings
     * emitted there just mean that you are talking to an old version of
     * Elasticsearch. There isn't anything you can do about the deprecation.
     */
    private static final DeprecationHandler DEPRECATION_HANDLER = DeprecationHandler.IGNORE_DEPRECATIONS;
   .....
   .....
}

错误本身表明我们要更新所有相关的 elasticsearch 库,虽然我找不到任何开箱即用的 elastic search BOM 来解决此版本冲突,但我已经完成了以下解决方法。

dependencies{

    implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.8.1'
    implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.8.1'
    implementation 'org.elasticsearch:elasticsearch:7.8.1'

}

//Since version 7.6.2 is selected by rule, substituting the version 7.8.1 as below

configurations.all {
    resolutionStrategy {
        dependencySubstitution {
            substitute module('org.elasticsearch.client:elasticsearch-rest-high-level-client') with module('org.elasticsearch.client:elasticsearch-rest-high-level-client:7.8.1')
            substitute module('org.elasticsearch.client:elasticsearch-rest-client') with module('org.elasticsearch.client:elasticsearch-rest-client:7.8.1')
            substitute module('org.elasticsearch:elasticsearch') with module('org.elasticsearch:elasticsearch:7.8.1')
            
      }
  }
}

【讨论】:

    【解决方案3】:

    你没有正确初始化你的 elasticsearch 客户端,你可以试试下面的代码:

    请注意,我正在使用接受主机、端口和http 方案的版本,它对我来说很好,是standard for creating the client.

    @Configuration
    @Primary
    public class ElasticsearchConfig {
    
        /**
         * Creates a Elasticsearch client from config
         *
         * @return Elasticsearch client
         */
        @Bean(destroyMethod = "close")
        public RestHighLevelClient client() {
            RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9500, "http")));
            return client;
        }
    }
    

    并使用下面的配置

    elasticsearch.host=localhost
    elasticsearch.port=9500
    

    【讨论】:

    • 没有解决问题,仍然面临同样的问题!
    • @DevChauhan 可以用7.6.0版本的rest客户端吗,也可以把你的代码上传到github上让我看看repro
    【解决方案4】:

    你可以为所有的Spring-boot设置版本:

    ext {
        set('elasticsearch.version', '6.2.0')
    }
    

    避免在多个地方覆盖它。

    【讨论】:

      猜你喜欢
      • 2017-10-15
      • 2016-10-22
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2023-04-08
      • 2020-11-04
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多