gpt4 book ai didi

java - ElasticSearch SpringBoot+Spring Data : java. lang.IllegalStateException:在接口(interface)上找不到合适的构造函数

转载 作者:行者123 更新时间:2023-12-02 11:04:33 27 4
gpt4 key购买 nike

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

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

最佳答案

在您的配置类中:

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

关于java - ElasticSearch SpringBoot+Spring Data : java. lang.IllegalStateException:在接口(interface)上找不到合适的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51079303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com