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

什鲁蒂·巴蒂亚

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

引起:java.lang.IllegalStateException:在接口 com.example.elasticSearchDemo.Repository.ESDemoRepository 上找不到合适的构造函数来匹配给定的参数:[class org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation,class 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);
}
拉法尔·索卡尔斯基

在您的配置类中:

@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. 在存储库类中使用双下划线

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章