Java 考虑定义一个名为“γx27”的bean;elasticsearchTemplate';在您的配置中
我刚刚启动了springboot,并试图用springboot实现弹性搜索,但我在运行springboot应用程序时遇到了这种类型的错误 考虑在配置中定义一个名为“elasticsearchTemplate”的bean POM.XMLJava 考虑定义一个名为“γx27”的bean;elasticsearchTemplate';在您的配置中,java,spring,spring-boot,
elasticsearch,elastic-stack,Java,Spring,Spring Boot,
elasticsearch,Elastic Stack,我刚刚启动了springboot,并试图用springboot实现弹性搜索,但我在运行springboot应用程序时遇到了这种类型的错误 考虑在配置中定义一个名为“elasticsearchTemplate”的bean POM.XML org.springframework.boot spring引导启动器数据jpa org.springframework.boot SpringBootStarterWeb org.springframework.boot 弹簧起动试验 测试 javax.se
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
javax.servlet
jstl
org.apache.tomcat.embed
汤姆卡特·贾斯珀
假如
org.springframework.boot
弹簧靴开发工具
org.webjars
独自创立
4.0.0
org.elasticsearch.client
运输
5.6.10
org.springframework.boot
弹簧启动启动器数据弹性搜索
存储库
@存储库
公共界面StudentRepository扩展ElasticsearchRepository{}
控制器
@RestController
公共类控制器{
@自动连线
StudentRepository studentRepo;
@GetMapping(value=“/student/all”)
列表getAllStudent(){
迭代器studentList=studentRepo.findAll().Iterator();
List students=new ArrayList();
if(studentList.hasNext()){
添加(studentList.next());
}
留学生;
}
@后期映射(value=“/student/add”)
字符串addStudent(@RequestBody Student){
学生储蓄;
返回“记录添加成功”;
}
@DeleteMapping(value=“/student/delete/{id}”)
字符串deleteStudent(@PathVariable int-id){
studentRepo.deleteById(id);
返回“记录删除成功”;
}
//@GetMapping(value=“/student/findById/{id}”)
}
有人能帮我解决这个错误吗
考虑在配置中定义一个名为“elasticsearchTemplate”的bean
您需要在application.properties文件中定义一些弹性搜索属性,例如
群集节点
,群集名称
,这些属性由ElasticsearchTemplate和ElasticsearchRepository用于连接到Elasticsearch引擎
您可以参考下面提到的链接:
注意:请参考or(检查所需版本的Elasticsearch版本)以检查版本兼容性。
解决方案(1):
如果您想使用SpringBoot1.x,只需创建一个
@Configuration
类并添加一个ElasticsearchOperations
Bean请注意,spring boot 1.x不支持ElasticSearch 5.x及更高版本的最新版本。
cluster.name:确保在代码中设置的集群名称与在$ES_HOME/config/elasticsearch.yml中设置的集群名称相同
@Configuration
public class ElasticSearchConfig {
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(getClient());
}
@Bean
public Client getClient() throws UnknownHostException {
Settings setting = Settings
.builder()
.put("client.transport.sniff", true)
.put("path.home", "/usr/share/elasticsearch") //elasticsearch home path
.put("cluster.name", "elasticsearch")
.build();
//please note that client port here is 9300 not 9200!
TransportClient client = new PreBuiltTransportClient(setting)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return client;
}
}
解决方案(2):此外,您还可以参考该解决方案,它显示了spring boot 2.2.0中spring数据中Elasticsearch的自动配置。
因此,使用
spring boot 2.2
和spring boot starter ElasticsRach
无需手动配置Elasticsearch。示例工作项目:
版本:
弹簧靴:2.2.0.释放
弹性搜索:6.6.2
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.jest.uris=http://localhost:9200
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "your_index", type = "books")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
//getter, setter/constructors
}
import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
List<Book> findByTitle(String title);
}
import com.example.model.Book;
import com.example.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
private BookRepository bookRepository;
public BookServiceImpl(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public Book save(Book book) {
return bookRepository.save(book);
}
@Override
public void delete(Book book) {
bookRepository.delete(book);
}
@Override
public Book findOne(String id) {
return bookRepository.findById(id).orElse(null);
}
@Override
public Iterable<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Page<Book> findByAuthor(String author, Pageable pageable) {
return bookRepository.findByAuthor(author, pageable);
}
@Override
public List<Book> findByTitle(String title) {
return bookRepository.findByTitle(title);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BookTest {
@Autowired
private BookService bookService;
@Autowired
private ElasticsearchTemplate esTemplate;
@Before
public void before(){
esTemplate.deleteIndex(Book.class);
esTemplate.createIndex(Book.class);
esTemplate.putMapping(Book.class);
esTemplate.refresh(Book.class);
}
@Test
public void testSave(){
Book book = new Book("1001", "Elasticsearch", "title", "23-FEB-2017");
Book testBook = bookService.save(book);
assertNotNull(testBook.getId());
assertEquals(testBook.getTitle(), book.getTitle());
assertEquals(testBook.getAuthor(), book.getAuthor());
assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
}
}
主应用程序类:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.jest.uris=http://localhost:9200
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "your_index", type = "books")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
//getter, setter/constructors
}
import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
List<Book> findByTitle(String title);
}
import com.example.model.Book;
import com.example.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
private BookRepository bookRepository;
public BookServiceImpl(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public Book save(Book book) {
return bookRepository.save(book);
}
@Override
public void delete(Book book) {
bookRepository.delete(book);
}
@Override
public Book findOne(String id) {
return bookRepository.findById(id).orElse(null);
}
@Override
public Iterable<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Page<Book> findByAuthor(String author, Pageable pageable) {
return bookRepository.findByAuthor(author, pageable);
}
@Override
public List<Book> findByTitle(String title) {
return bookRepository.findByTitle(title);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BookTest {
@Autowired
private BookService bookService;
@Autowired
private ElasticsearchTemplate esTemplate;
@Before
public void before(){
esTemplate.deleteIndex(Book.class);
esTemplate.createIndex(Book.class);
esTemplate.putMapping(Book.class);
esTemplate.refresh(Book.class);
}
@Test
public void testSave(){
Book book = new Book("1001", "Elasticsearch", "title", "23-FEB-2017");
Book testBook = bookService.save(book);
assertNotNull(testBook.getId());
assertEquals(testBook.getTitle(), book.getTitle());
assertEquals(testBook.getAuthor(), book.getAuthor());
assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
}
}
型号类别:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.jest.uris=http://localhost:9200
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "your_index", type = "books")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
//getter, setter/constructors
}
import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
List<Book> findByTitle(String title);
}
import com.example.model.Book;
import com.example.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
private BookRepository bookRepository;
public BookServiceImpl(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public Book save(Book book) {
return bookRepository.save(book);
}
@Override
public void delete(Book book) {
bookRepository.delete(book);
}
@Override
public Book findOne(String id) {
return bookRepository.findById(id).orElse(null);
}
@Override
public Iterable<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Page<Book> findByAuthor(String author, Pageable pageable) {
return bookRepository.findByAuthor(author, pageable);
}
@Override
public List<Book> findByTitle(String title) {
return bookRepository.findByTitle(title);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BookTest {
@Autowired
private BookService bookService;
@Autowired
private ElasticsearchTemplate esTemplate;
@Before
public void before(){
esTemplate.deleteIndex(Book.class);
esTemplate.createIndex(Book.class);
esTemplate.putMapping(Book.class);
esTemplate.refresh(Book.class);
}
@Test
public void testSave(){
Book book = new Book("1001", "Elasticsearch", "title", "23-FEB-2017");
Book testBook = bookService.save(book);
assertNotNull(testBook.getId());
assertEquals(testBook.getTitle(), book.getTitle());
assertEquals(testBook.getAuthor(), book.getAuthor());
assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
}
}
存储库类:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.jest.uris=http://localhost:9200
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "your_index", type = "books")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
//getter, setter/constructors
}
import com.example.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {
Page<Book> findByAuthor(String author, Pageable pageable);
List<Book> findByTitle(String title);
}
import com.example.model.Book;
import com.example.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
private BookRepository bookRepository;
public BookServiceImpl(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
@Override
public Book save(Book book) {
return bookRepository.save(book);
}
@Override
public void delete(Book book) {
bookRepository.delete(book);
}
@Override
public Book findOne(String id) {
return bookRepository.findById(id).orElse(null);
}
@Override
public Iterable<Book> findAll() {
return bookRepository.findAll();
}
@Override
public Page<Book> findByAuthor(String author, Pageable pageable) {
return bookRepository.findByAuthor(author, pageable);
}
@Override
public List<Book> findByTitle(String title) {
return bookRepository.findByTitle(title);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BookTest {
@Autowired
private BookService bookService;
@Autowired
private ElasticsearchTemplate esTemplate;
@Before
public void before(){
esTemplate.deleteIndex(Book.class);
esTemplate.createIndex(Book.class);
esTemplate.putMapping(Book.class);
esTemplate.refresh(Book.class);
}
@Test
public void testSave(){
Book book = new Book("1001", "Elasticsearch", "title", "23-FEB-2017");
Book testBook = bookService.save(book);
assertNotNull(testBook.getId());
assertEquals(testBook.getTitle(), book.getTitle());
assertEquals(testBook.getAuthor(), book.getAuthor());
assertEquals(testBook.getReleaseDate(), book.getReleaseDate());
}
}