考虑在配置中定义一个名为"elasticsearchTemplate"的bean [英] Consider defining a bean named 'elasticsearchTemplate' in your configuration
问题描述
我刚刚启动springboot并尝试通过spring-boot实现弹性搜索,但是在运行spring-boot应用程序时出现此类错误
I have just started springboot and tried to implement elastic search with spring-boot but I am getting this type of error while running spring-boot app
考虑在您的配置中定义一个名为"elasticsearchTemplate"的bean.
Consider defining a bean named 'elasticsearchTemplate' in your configuration.
POM.XML
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
存储库
@Repository
public interface StudentRepository extends ElasticsearchRepository<Student, Integer>{}
控制器
@RestController
public class Controller {
@Autowired
StudentRepository studentRepo;
@GetMapping(value="/student/all")
List<Student> getAllStudent() {
Iterator<Student> studentList = studentRepo.findAll().iterator();
List<Student> students = new ArrayList<>();
if(studentList.hasNext()) {
students.add(studentList.next());
}
return students;
}
@PostMapping(value="/student/add")
String addStudent(@RequestBody Student student) {
studentRepo.save(student);
return "Record Added Successfully";
}
@DeleteMapping(value="/student/delete/{id}")
String deleteStudent(@PathVariable int id) {
studentRepo.deleteById(id);
return "Record Deleted Successfully";
}
//@GetMapping(value="/student/findById/{id}")
}
任何人都可以帮助我解决此错误
Can Anyone help me to resolve this error
考虑在您的配置中定义一个名为"elasticsearchTemplate"的bean.
Consider defining a bean named 'elasticsearchTemplate' in your configuration.
推荐答案
您需要在application.properties文件中定义一些弹性搜索属性,例如 cluster-nodes
, cluster-names
,由 ElasticsearchTemplate 和 ElasticsearchRepository 使用以连接到Elasticsearch引擎.
You need to define some elastic search properties in your application.properties file such as cluster-nodes
, cluster-names
which are used by ElasticsearchTemplate and ElasticsearchRepository to connect to the Elasticsearch engine.
您可以参考以下提到的链接:
You can refer below mentioned link :
https://dzone.com/articles/elasticsearch-with-spring-启动应用程序
这篇关于考虑在配置中定义一个名为"elasticsearchTemplate"的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!