3

Problem

It seems like my SpringBoot application can't connect with ElasticSearch. I keep getting this error whenever i try to run my application:

UnsatisfiedDependencyException: Error creating bean with name 'filterController': Unsatisfied dependency expressed through field 'service': Error creating bean with name 'filterService': Unsatisfied dependency expressed through field 'repository': Error creating bean with name 'filterRepository' defined in repository.FilterRepository defined in @EnableElasticsearchRepositories declared on ElasticsearchRepositoriesRegistrar.EnableElasticsearchRepositoriesConfiguration: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception

Code info

  • I didn't make any changes nor extra-configurations. I'm running ElasticSearch normally
  • I did not create a custom configuration file, since SpringBoot does it automatically
  • You can find full source-code here

This is my Application.java file:

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);
    }

}

This is my FilterController.java file:

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
    
}

This is the FilterService.java file:

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);
    }
    
}

My FilterRepository.java file:

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> {}

To finish, my project's dependencies:

<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>

Some solutions i've tried:

  • Initializing my service and repository variables without the @Autowired tag
  • Initializing it inside a class constructor, passing the @Autowired tag above the constructor
  • Running ElasticSearch as admin
  • Cleaning Java Language Server Workspace (I'm using VSCode)
  • Running mvn clean install once again before trying to run my application

Logs from ElasticSearch

I've noticed this logs in the ElasticSearch's console whenever i try to run my SpringBoot application:

[2023-04-23T14:57:56,085][WARN ][o.e.h.n.Netty4HttpServerTransport] [DESKTOP-LO4MIAG] received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{localAddress=/127.0.0.1:9200, remoteAddress=/127.0.0.1:53007}

2 Answers2

2

plz change the index name to the lowercase in class below:

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";
    
}

Result:

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)
Theara
  • 79
  • 2
  • 12
  • Sadly, it didn't work. I've tried running ElasticSearch as admin and, by your comment, i though it could be a problem with the path in the @RequestMapping tag and changed it to match the values in the index file, but none of it worked. The only thing i've noticed is the following log in elasticsearch's console whenever i try to run my aplication: [2023-04-23T14:57:56,085][WARN ][o.e.h.n.Netty4HttpServerTransport] [DESKTOP-LO4MIAG] received plaintext http traffic on an https channel, closing connection Netty4HttpChannel{localAddress=/127.0.0.1:9200, remoteAddress=/127.0.0.1:53007} – Dheovani Xavier Apr 23 '23 at 18:00
1

Almost 2 months later, finally managed to solve it. It was a problem with the ssl connection. I've turned it all off and reconfigured it manually in elasticsearch-8.7.0\config\elasticsearch.yml to make it work