如何将 Patch 方法与连接到 Couchbase 数据库的 Spring Boot Rest API 一起使用?
How can I use Patch method with Spring Boot Rest APIs connected to Couchbase database?
我正在尝试设计一个 Spring 引导应用程序,将产品保存在数据库中并可以执行基本的 CRUD 操作。 Post、Get、Put、Delete 等方法都可以正常工作,但是当我尝试 Patch 方法时,我无法得到正确的结果。我尝试按照 Medium post here.
中提到的路径进行操作
但与此不同的是,我还想与Couchbase数据库进行通信。当我尝试修补数据库中的数据时,返回以下错误消息。在此先感谢大家。
{
"timestamp": "2020-10-14T21:46:50.599+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/v1/products/f37e4e07-1301-43c1-afae-04eba1b9b437"
}
这是我的 Java 代码:
- 我的产品域名class:
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product {
private String id;
private String category;
private double price;
private String description;
private int quantity;
}
- 产品库:
@Repository
public class ProductRepository {
private final Cluster couchbaseCluster;
private final Collection productCollection;
public ProductRepository(Cluster couchbaseCluster, Collection productCollection) {
this.couchbaseCluster = couchbaseCluster;
this.productCollection = productCollection;
}
public void saveProduct(Product product){
if (product == null){
throw new IllegalArgumentException("Product can't be null");
}
if (!findProductById(product.getId()).equals(product)) {
throw new RuntimeException(String.format("Product with id '%s' does not exists", product.getId()));
}
productCollection.remove(product.getId());
productCollection.insert(product.getId(), product);
}
}
- 产品服务:
@Service
public class ProductService {
private final ProductRepository productRepository;
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public void save(Product product) {
try {
productRepository.saveProduct(product);
} catch (Exception e) {
throw new IllegalArgumentException();
}
}
}
- ProductUpdateDTO class:
@Getter
public class ProductUpdateDTO {
private String category;
private Double price;
private String description;
private Integer quantity;
protected ProductUpdateDTO(){ }
}
- 最后,产品负责人:
@RestController
@RequestMapping("/v1/products")
public class ProductController {
private final ProductService productService;
private final RestService restService;
public ProductController(ProductService productService, RestService restService) {
this.productService = productService;
this.restService = restService;
}
@PatchMapping ("/{id}")
public ResponseEntity<Void> updateProduct(@PathVariable("id") String productId,
@RequestBody ProductUpdateDTO productUpdateDTO) {
Optional<Product> productOptional = productService.findProductById(productId);
if (productOptional.isEmpty()) {
return ResponseEntity.notFound().build();
}
Product product = productOptional.get();
if (productUpdateDTO.getCategory() != null) {
product.setCategory(productUpdateDTO.getCategory());
}
if (productUpdateDTO.getDescription() != null) {
product.setDescription(productUpdateDTO.getDescription());
}
if (productUpdateDTO.getPrice() != null) {
product.setPrice(productUpdateDTO.getPrice());
}
if (productUpdateDTO.getQuantity() != null) {
product.setQuantity(productUpdateDTO.getQuantity());
}
productService.save(product);
return ResponseEntity.noContent().build();
}
}
可选 class 有误。当我在第 2 部分的代码块中进行以下更改时,问题就解决了。
@Repository
public class ProductRepository {
private final Cluster couchbaseCluster;
private final Collection productCollection;
public ProductRepository(Cluster couchbaseCluster, Collection productCollection) {
this.couchbaseCluster = couchbaseCluster;
this.productCollection = productCollection;
}
public void saveProduct(Product product){
if (product == null){
throw new IllegalArgumentException("Product can't be null");
}
---> ****if (findProductById(product.getId()).isEmpty())**** {
throw new RuntimeException(String.format("Product with id '%s' does not exists", product.getId()));
}
productCollection.remove(product.getId());
productCollection.insert(product.getId(), product);
}
}
我正在尝试设计一个 Spring 引导应用程序,将产品保存在数据库中并可以执行基本的 CRUD 操作。 Post、Get、Put、Delete 等方法都可以正常工作,但是当我尝试 Patch 方法时,我无法得到正确的结果。我尝试按照 Medium post here.
中提到的路径进行操作但与此不同的是,我还想与Couchbase数据库进行通信。当我尝试修补数据库中的数据时,返回以下错误消息。在此先感谢大家。
{
"timestamp": "2020-10-14T21:46:50.599+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/v1/products/f37e4e07-1301-43c1-afae-04eba1b9b437"
}
这是我的 Java 代码:
- 我的产品域名class:
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product {
private String id;
private String category;
private double price;
private String description;
private int quantity;
}
- 产品库:
@Repository
public class ProductRepository {
private final Cluster couchbaseCluster;
private final Collection productCollection;
public ProductRepository(Cluster couchbaseCluster, Collection productCollection) {
this.couchbaseCluster = couchbaseCluster;
this.productCollection = productCollection;
}
public void saveProduct(Product product){
if (product == null){
throw new IllegalArgumentException("Product can't be null");
}
if (!findProductById(product.getId()).equals(product)) {
throw new RuntimeException(String.format("Product with id '%s' does not exists", product.getId()));
}
productCollection.remove(product.getId());
productCollection.insert(product.getId(), product);
}
}
- 产品服务:
@Service
public class ProductService {
private final ProductRepository productRepository;
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public void save(Product product) {
try {
productRepository.saveProduct(product);
} catch (Exception e) {
throw new IllegalArgumentException();
}
}
}
- ProductUpdateDTO class:
@Getter
public class ProductUpdateDTO {
private String category;
private Double price;
private String description;
private Integer quantity;
protected ProductUpdateDTO(){ }
}
- 最后,产品负责人:
@RestController
@RequestMapping("/v1/products")
public class ProductController {
private final ProductService productService;
private final RestService restService;
public ProductController(ProductService productService, RestService restService) {
this.productService = productService;
this.restService = restService;
}
@PatchMapping ("/{id}")
public ResponseEntity<Void> updateProduct(@PathVariable("id") String productId,
@RequestBody ProductUpdateDTO productUpdateDTO) {
Optional<Product> productOptional = productService.findProductById(productId);
if (productOptional.isEmpty()) {
return ResponseEntity.notFound().build();
}
Product product = productOptional.get();
if (productUpdateDTO.getCategory() != null) {
product.setCategory(productUpdateDTO.getCategory());
}
if (productUpdateDTO.getDescription() != null) {
product.setDescription(productUpdateDTO.getDescription());
}
if (productUpdateDTO.getPrice() != null) {
product.setPrice(productUpdateDTO.getPrice());
}
if (productUpdateDTO.getQuantity() != null) {
product.setQuantity(productUpdateDTO.getQuantity());
}
productService.save(product);
return ResponseEntity.noContent().build();
}
}
可选 class 有误。当我在第 2 部分的代码块中进行以下更改时,问题就解决了。
@Repository
public class ProductRepository {
private final Cluster couchbaseCluster;
private final Collection productCollection;
public ProductRepository(Cluster couchbaseCluster, Collection productCollection) {
this.couchbaseCluster = couchbaseCluster;
this.productCollection = productCollection;
}
public void saveProduct(Product product){
if (product == null){
throw new IllegalArgumentException("Product can't be null");
}
---> ****if (findProductById(product.getId()).isEmpty())**** {
throw new RuntimeException(String.format("Product with id '%s' does not exists", product.getId()));
}
productCollection.remove(product.getId());
productCollection.insert(product.getId(), product);
}
}