SpringBoot集成SpringBootDataElasticSearch

先放出依賴:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 

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

 

spring-boot-starter-web 這個依賴需要引入,是因爲ElasticSearch設置HttpHeaders的時候,需要這個包中的一個類而已。。。


放出配置Bean代碼;這個HttpHeaders是 import org.springframework.http.HttpHeaders; 包中的,引入上面的
spring-boot-starter-web 
依賴就可以了
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        
        final ClientConfiguration  clientConfiguration = ClientConfiguration.builder()  
            .connectedTo("localhost:9200").withDefaultHeaders(httpHeaders).build();

        return RestClients.create(clientConfiguration).rest();                         
    }
    
}

 

 

 

 應該是官方文檔沒有完善好吧,這些小問題只能自己研究了。。。。。有問題評論留言討論

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章