1. 确保POM文件依赖正确
首先,确保你在pom.xml
文件中正确地添加了Spring Data Elasticsearch和Elasticsearch的依赖:
XML
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data Elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- Elasticsearch client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.6</version>
</dependency>
</dependencies>
2. 配置Elasticsearch连接
在application.properties
文件中配置Elasticsearch的连接信息:
java
spring.data.elasticsearch.client.reactive.endpoints=localhost:9200
spring.data.elasticsearch.client.reactive.use-ssl=false
spring.elasticsearch.uris=http://localhost:9200
3. 创建Elasticsearch实体类
创建一个名为Product
的实体类:
java
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@Document(indexName = "product")
public class Product {
@Id
private String id;
private String name;
private Double price;
}
4. 创建存储库接口
创建一个名为ProductRepository
的存储库接口:
java
import com.example.demo.model.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
}
5. 创建控制器
创建一个名为ProductController
的REST控制器,定义基本的CRUD操作:
java
import com.example.demo.model.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductRepository productRepository;
@PostMapping
public Product createProduct(@RequestBody Product product) {
return productRepository.save(product);
}
@GetMapping("/{id}")
public Product getProductById(@PathVariable String id) {
Optional<Product> product = productRepository.findById(id);
return product.orElse(null);
}
@PutMapping("/{id}")
public Product updateProduct(@PathVariable String id, @RequestBody Product product) {
product.setId(id);
return productRepository.save(product);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable String id) {
productRepository.deleteById(id);
}
@GetMapping
public Iterable<Product> getAllProducts() {
return productRepository.findAll();
}
}
6. 启动Elasticsearch服务
确保你已经在本地或远程启动了Elasticsearch服务,默认端口为9200
。
7. 运行Spring Boot应用
启动Spring Boot应用,访问以下端点测试基本的CRUD操作:
java
创建产品(POST请求):http://localhost:8080/products
获取产品(GET请求):http://localhost:8080/products/{id}
更新产品(PUT请求):http://localhost:8080/products/{id}
删除产品(DELETE请求):http://localhost:8080/products/{id}
获取所有产品(GET请求):http://localhost:8080/products
测试请求
创建产品,post请求不指定id会自动生成id并返回
java
POST http://localhost:8080/products
Content-Type: application/json
{
"name": "Laptop",
"price": 1200.00
}
返回结果:这里的id是自动生成的id,给下面的接口使用
java
{
"id": "r00DPpIBab4_XTIP0FK9",
"name": "Laptop",
"price": 1200.0
}
获取产品
java
GET http://localhost:8080/products/{id}
即
GET http://localhost:8080/products/r00DPpIBab4_XTIP0FK9
更新产品
java
PUT http://localhost:8080/products/{id}
Content-Type: application/json
{ "name": "Laptop Pro", "price": 1500.00 }
删除产品
java
DELETE http://localhost:8080/products/{id}
获取所有产品
java
GET http://localhost:8080/products