java - ElasticSearch SpringBoot + Spring Data: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
有两种解决方案:
- 使用驼峰式大小写而不是使用下划线。
- 在存储库类中使用双下划线
推荐阅读
- c++ - 如何解决分段错误单链表?
- r - 将命名向量传递到 str_replace_all 以替换多个模式
- typescript - 无法窥探原始值;给定的未定义
- caching - Cloudflare 按前缀清除缓存不起作用
- css - 脉轮卡组件是垂直映射而不是水平映射
- spring - 用于在反序列化期间忽略未知属性的 SpringMVC 全局设置
- javascript - Angular 不更新 *ngFor 循环
- python - 如何绘制具有多个数据集的多个分类器的准确性
- python - 由于日期时间比较,根据时间框架中断过滤 icalendar 事件
- python - 用于从新的 www.ffiec.gov 站点抓取银行控股公司 (FR Y-9C) 数据的 Curl 命令(或无头 python 替代方案)?