简体   繁体   中英

ElasticSearch SpringBoot+Spring Data: java.lang.IllegalStateException: No suitable constructor found on interface

I am trying to extend ElasticsearchRepository in my project but I am unable to due to the following error

Caused by: java.lang.IllegalStateException: No suitable constructor found on interface com.example.elasticSearchDemo.Repository.ESDemoRepository to match the given arguments: [class org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation, class org.springframework.data.elasticsearch.core.ElasticsearchTemplate]. Make sure you implement a constructor taking these

//My interface that extends ElasticSearchRepository:

public interface ESDemoRepository extends ElasticsearchRepository<Data, String>{
    Page<Data> findByEsblog_flow_name(String flow_name, Pageable pageable);

    List<Data> findByEsblog_type(String type);
}

//Data class
@Document(indexName = "logdata", type="doc")
public class Data {
 @Id
 private String id;
 private String esblog_time;
 private String esblog_appl_name;
 private String esblog_host_name;
 private String esblog_iib_name;
 private String esblog_flow_name;
 private String esblog_payload;
 private String esblog_type;
 private Integer esblog_retention;
 private String esblog_tansaction_id;

@Override
public String toString() {
    return "Data{" +
            "id='" + id + '\'' +
            ", time='" + esblog_time + '\'' +
            ", payload='" + esblog_payload + '\'' +
            ", type='" + esblog_type + '\'' +
            ", retention='" + esblog_retention + '\'' +
            '}';
}

}

@Configuration
@EnableElasticsearchRepositories(repositoryBaseClass = 
ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")
public class ESConfiguration {
@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;


@Bean
public Client client() throws Exception {

    Settings settings = Settings.builder()
            .put("cluster.name", EsClusterName).build();
 return  new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

}

@Service
public class DemoServiceImpl implements DemoService {



private ESDemoRepository demoRepository;
@Autowired
public void setDemoRepository(ESDemoRepository demoRepository){this.demoRepository=demoRepository;}

private ESDemoDAO esDemoDAO;
@Autowired
public void setEsDemoDAO(ESDemoDAO demoDAO){this.esDemoDAO=demoDAO;}

public String save(Data data) throws Exception {
    return esDemoDAO.esQueryDemo(data);
}

public void delete(Data data) {
    demoRepository.delete(data);
}

public Data findOne(String id) throws Exception {
    return esDemoDAO.esGetQuery(id);
}

public Iterable<Data> findAll() {
    return demoRepository.findAll();
}


public List<Data> findByType(String type) {
    return demoRepository.findByEsblog_type(type);
}

In your configuration class:

@Configuration
@EnableElasticsearchRepositories(repositoryBaseClass = 
ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")
public class ESConfiguration {
@Value("${elasticsearch.host}")
private String EsHost;

@Value("${elasticsearch.port}")
private int EsPort;

@Value("${elasticsearch.clustername}")
private String EsClusterName;


@Bean
public Client client() throws Exception {

    Settings settings = Settings.builder()
            .put("cluster.name", EsClusterName).build();
 return  new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

Replace:

 @EnableElasticsearchRepositories(repositoryBaseClass = 
    ESDemoRepository.class,basePackages="com.example.elasticSearchDemo")

With this:

 @EnableElasticsearchRepositories(basePackages="com.example.elasticSearchDemo")

EDIT

After this changes there is exception :

error:Caused by: org.springframework.data.mapping.PropertyReferenceException: No property esblog found for type Data!

It's because of bad name convention, because the underscore _ is a reserved character in Spring Data. According to:

https://stackoverflow.com/a/23475349/6003541

There are two solutions:

  1. Use camel case instead of using underscore.
  2. Use double underscore in repository class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM