Spring Boot 2.3.1 弹性搜索 7.6.2

GIU*_*IO 2 java elasticsearch spring-boot spring-boot-starter

我正在尝试制作一个连接到 elastichsearch 实例的简单 springboot 项目。我必须使用 springboot 2.3.1 和 elasticsearch 7.6.2。我用 springinitialiazr 创建了我的项目,相对的 pom.xml 是:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoElastic</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

我已经按照一些在线指南的建议创建了一个 Config.class

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.repository")
@ComponentScan(basePackages = { "com.example.demo.service" })
public class Config {
 
    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
 
        return RestClients.create(clientConfiguration).rest();
    }
 
    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}
Run Code Online (Sandbox Code Playgroud)

应该连接到我的elasticsearch 7.6.2 istance

当我运行我的 empy 应用程序时,我收到此错误。

启动 ApplicationContext 时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。2020-07-02 12:07:19.061 错误 8500 --- [main] osboot.SpringApplication:应用程序运行失败 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com] 中创建名为“client”的 bean 时出错/example/demo/config/Config.class]:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.elasticsearch.client.RestHighLevelClient]:工厂方法“client”抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/http/HttpHeaders at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java: springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3 .1.RELEASE] 在 com.example.demo.DemoElasticApplication.main(DemoElasticApplication.java:10) [classes/:na] 引起:org.springframework.beans.BeanInstantiationException:无法实例化 [org.elasticsearch.client.RestHighLevelClient ]:工厂方法'client'抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/http/HttpHeaders at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:

小智 5

我认为你缺少这种依赖

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

因为在错误日志中它指出错误是

java.lang.NoClassDefFoundError: org/springframework/http/HttpHeaders