Search code examples
javaspring-bootelasticsearchexceptionindexing

SpringBoot can't connect to ElasticSearch. It failed to instantiate SimpleElasticsearchRepository


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}


Solution

  • 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