SpringBoot无法连接到ElasticSearch,示例化SimpleElasticsearchRepository失败

tzcvj98z  于 2023-04-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(779)

问题

我的SpringBoot应用程序似乎无法连接ElasticSearch。每当我尝试运行我的应用程序时,我总是收到这个错误:
UnsatisfiedDependencyException:创建名为“filterController”的bean时出错:通过字段“服务”表示的未满足的依赖关系:创建名为“filterService”的bean时出错:通过字段“repository”表示的未满足的依赖关系:创建存储库中定义的名为“filterRepository”的Bean时出错。在ElasticsearchRepositoriesRegistrar上声明的@EnableElasticsearchRepositories中定义的FilterRepository。启用ElasticsearchRepositoriesConfiguration:示例化[org]失败。SpringFrameworkdata.elasticsearch.repository.support.SimpleElasticsearchRepository]:构造函数引发异常

代码信息

  • 我没有做任何改变,也没有额外的配置。ElasticSearch正常运行
  • 我没有创建自定义配置文件,因为SpringBoot does it automatically
  • 您可以找到完整的源代码here

这是我 www.example.com 文件:

package br.com.uburu.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

这是我 www.example.com 文件:

package br.com.uburu.spring.controller;

// Imports

@RestController
@RequestMapping("/api/v1/filter")
public class FilterController {

    @Autowired
    private FilterService service;

    @GetMapping
    public ResponseEntity<List<Filter>> getAll() {
        List<Filter> filters = service.getAll();
        return new ResponseEntity<List<Filter>>(filters, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Filter> getById(@PathVariable("id") long id) {
        Filter filter = service.findById(id);
        return new ResponseEntity<Filter>(filter, HttpStatus.OK);
    }

    // Some other methods
    
}

这是 www.example.com 文件:

package br.com.uburu.spring.service;

// Imports

@Service
public class FilterService {

    @Autowired
    private FilterRepository repository;

    public List<Filter> getAll() {
        List<Filter> filters = new ArrayList<>();
        repository.findAll().forEach(filters::add);

        return filters;
    }

    public Filter findById(long id) {
        return repository.findById(id).orElse(null);
    }

    public Filter save(Filter filter) {
        return repository.save(filter);
    }

    public void delete(long id) {
        repository.deleteById(id);
    }
    
}

我的 www.example.com 文件:

package br.com.uburu.spring.repository;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import br.com.uburu.spring.document.Filter;

public interface FilterRepository extends ElasticsearchRepository<Filter, Long> {}

最后,我的项目的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
        
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>5.0.5</version>
    </dependency>
</dependencies>

我尝试过的一些解决方案:

  • 初始化我的服务和存储库变量,不带@Autowired标记
  • 在类构造函数中初始化它,在构造函数上方传递@Autowired标记
  • 以admin身份运行ElasticSearch
  • 清理Java语言服务器工作区(我正在使用VSCode)
  • 在尝试运行应用程序之前再次运行mvn clean install

ElasticSearch日志

我注意到,每当我尝试运行我的SpringBoot应用程序时,ElasticSearch的控制台中都会出现以下日志:
[2019 -04- 15 00:01:00][2019 - 04 - 15 00:00:00][2019 - 04 - 15 00:00][2019 - 04 - 15 00:00][2019 - 04 - 15 00:00][2019 - 01:00][2019 - 01][2019 - 01][2019 - 01:00][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01][2019 - 01]e.h.n.Netty4HttpServerTransport] [DESKTOP-LO 4 MIAG]在https通道上接收明文http流量,关闭连接Netty 4 HttpChannel {localAddress=/127。0.0.1:9200,remoteAddress=/127。0.0.1:53007}

uemypmqf

uemypmqf1#

请将下面的类中的索引名称更改为小写:

package br.com.uburu.spring.utils;

public final class Indices {

    public static final String KEYWORD = "keyword";
    public static final String PATH = "path";
    public static final String FILTER = "filter";
    
}

结果:

request [HEAD http://localhost:9200/filter] returned 1 warnings: [299 Elasticsearch-7.17.0-bee86328705acaa9a6daede7140defd4d9ec56bd "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."]
2023-04-23T14:40:01.302+07:00  WARN 75565 --- [  restartedMain] org.elasticsearch.client.RestClient      : request [HEAD http://localhost:9200/filter] returned 1 warnings: [299 Elasticsearch-7.17.0-bee86328705acaa9a6daede7140defd4d9ec56bd "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."]
2023-04-23T14:40:01.306+07:00  WARN 75565 --- [  restartedMain] org.elasticsearch.client.RestClient      : request [HEAD http://localhost:9200/filter] returned 1 warnings: [299 Elasticsearch-7.17.0-bee86328705acaa9a6daede7140defd4d9ec56bd "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security."]
2023-04-23T14:40:01.319+07:00  WARN 75565 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-04-23T14:40:01.433+07:00  INFO 75565 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-04-23T14:40:01.438+07:00  INFO 75565 --- [  restartedMain] br.com.uburu.spring.Application          : Started Application in 1.272 seconds (process running for 6.551)

相关问题