【发布时间】:2019-07-10 08:00:18
【问题描述】:
我使用一个小型 API,在我将所有数据保存在数据库中之前,我可以在该 API 中相应地打印它们。因此,似乎代码按预期工作,但是,我无法将它们保存在 MySQL 中。提供了实体类,
@Entity
public class Product {
@Id
@Column(name = "id")
private String id;
@Column(insertable = false, updatable = false, name = "timestamp")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
private java.sql.Timestamp timestamp;
@Embedded
private Stock stock;
public Product() {
}
public Product(String id, Timestamp timestamp, Stock stock) {
this.id = id;
this.timestamp = timestamp;
this.stock = stock;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public Stock getStock() {
return stock;
}
public void setStock(Stock stock) {
this.stock = stock;
}
}
@Embeddable
public class Stock {
@Id
@Column(name = "id")
private String id;
@Column(name = "timestamp")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private java.sql.Timestamp timestamp;
@Column(name = "quantity")
private int quantity;
public Stock() {
}
public Stock(String id, Timestamp timestamp, int quantity) {
this.id = id;
this.timestamp = timestamp;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
下面提供API,
@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {
@Autowired
private ProductService service;
/*
*
* $ curl -i -X POST -H "Content-Type:application/json" -d "{ \"id\": \"Product1 ID\", \"timestamp\": \"2017-07-16 22:54:01.754\", \"stock\" : { \"id\": \"Stock ID\", \"timestamp\": \"3000-07-16 22:54:01.754\", \"quantity\": \"350\" }}" http://localhost:8080/api/v1/products/createProduct
* */
@PostMapping(value = "/createProduct", consumes = "application/json", produces = "application/json")
public ResponseEntity<Product> createProduct(@RequestBody Product product) {
System.out.println("\n");
System.out.println("Product ID " + product.getId());
System.out.println("Product timestamp " +product.getTimestamp());
System.out.println("Stock ID " +product.getStock().getId());
System.out.println("Stock timestamp " +product.getStock().getTimestamp());
System.out.println("Stock quantity " +product.getStock().getQuantity());
System.out.println("\n");
service.save(product);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}
}
我为数据库中的持久化操作做了这个 cURL 调用,
$ curl -i -X POST -H "Content-Type:application/json" -d "{ \"id\": \"Product1 ID\", \"timestamp\": \"2017-07-16 22:54:01.754\", \"stock\" : { \"id\": \"Stock ID\", \"timestamp\": \"3000-07-16 22:54:01.754\", \"quantity\": \"350\" }}" http://localhost:8080/api/v1/products/createProduct
我的打印显示所有数据都如愿以偿,
Product ID Product1 ID
Product timestamp 2017-07-17 00:54:01.754
Stock ID Stock ID
Stock timestamp 3000-07-17 00:54:01.754
Stock quantity 350
这很好,但是,当我查看 MySQL 数据库时,我发现缺少产品时间戳和库存 ID。
对于产品时间戳,我需要使用insertable = false, updatable = false,否则会出错。不知道为什么 Stock id 不存在。如果需要,我可以提供 repo 和服务代码。
如何在 MySQL 中正确保存数据?
更新
当我从 Stock 中删除 @Id 注释并将其重命名为 id1 时,我能够将值保存到数据库中。我仍然无法将产品的时间戳保存到数据库中。
【问题讨论】:
标签: java mysql rest api spring-boot