ElasticSearch SpringBoot + Spring数据:java.lang.IllegalStateException:在接口上找不到合适的构造函数

时间:2018-06-28 09:27:59

标签: java spring-boot elasticsearch

我正在尝试在我的项目中扩展ElasticsearchRepository,但是由于以下错误,我无法这样做

  

由于:java.lang.IllegalStateException:没有合适的构造函数   在界面上找到   com.example.elasticSearchDemo.Repository.ESDemoRepository来匹配   给定参数:[类   org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation,   类   org.springframework.data.elasticsearch.core.ElasticsearchTemplate]。   确保使用这些实现一个构造器

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

1 个答案:

答案 0 :(得分:1)

在您的配置类中:

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

替换:

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

与此:

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

编辑

此更改之后有一个例外:

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

这是由于名称约定错误,因为下划线_是Spring Data中的保留字符。根据:

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

有两种解决方案:

  1. 使用驼峰式大小写代替下划线。
  2. 在存储库类中使用双下划线