No qualifying bean of type ‘org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate‘ a

报错

No qualifying bean of type ‘org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

提示找不到 ElasticsearchRestTemplate

解决方法

写一个配置类注入

package com.xjhqre.search.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        HttpHost host = new HttpHost("es的ip地址", 9200, HttpHost.DEFAULT_SCHEME_NAME);
        RestClientBuilder builder = RestClient.builder(host);

        // 若elasticsearch有密码则配置
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("账号", "密码"));
        builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));

        return new RestHighLevelClient(builder);
    }

     @Bean
     public ElasticsearchRestTemplate elasticsearchRestTemplate() {
     return new ElasticsearchRestTemplate(this.elasticsearchClient());
     }
}

你可能感兴趣的:(踩坑记录,elasticsearch,java,spring,boot)