我在下面提供了2个实体:
@Entity
public class Product {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "product_id")
private String id;
@Column
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Timestamp timestamp;
@OneToOne(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
private Stock stock;
}
@Entity
public class Stock {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "stock_id")
private String id;
@Column
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Timestamp timestamp;
@Column
private int quantity;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
private Product product;
}
我的目的是在数据库中插入一个产品对象,所以,如果以后我使用GET
命令,我将能够检索类似于的JSON
:
{
"productId": “string", // id of the requested product, e.g. "vegetable-123"
"requestTimestamp": “dateTime", // datetime in UTC when requested the stock
"stock": {
"id": "string",
"timestamp":
"dateTime" "quantity": "integer"
}
}
POST
电话的API如下:
@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {
@Autowired
private ProductService service;
@PostMapping(value = "/createProduct", consumes = "application/json", produces = "application/json")
public ResponseEntity<Product> createProduct(@RequestBody Product product) {
service.save(product);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}
}
提供cURL
请求,
$ curl -i -X POST -H "Content-Type:application/json" -d "{\"id\" :
\"Product ID\",\"timestamp\" : \"2017-07-16 22:54:01.754\",\"id\":
\"Stock ID\", \"timestamp\":\"2000-07-16 22:54:01.754\", \"quantity\":
\"250\"}" http://localhost:8080/api/v1/products/createProduct
命令成功并提供输出,
HTTP/1.1 201
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 15 Feb 2019 09:10:59 GMT
{"timestamp":"2000-07-16 22:54:01.754"}
但是,数据库条目不正确,
如何正确编写cURL POST请求?
使用CURL命令,我想用数据填充表,响应应该返回相同的,
{“productId”:“产品ID”“requestTimestamp”:“2017-07-16 22:54:01.754”
“股票”: {
"id": "Stock ID",
"timestamp": "2000-07-16 22:54:01.754",
"quantity": "250"
} }
看起来Timestamp
字段没有被反序列化,你需要用Timestamp
注释@JsonFormat
字段,例如:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
以下是一个例子:
public static void main(String[] args) throws Exception {
String s = "{\"timestamp\":\"2000-07-16 22:54:01.754\"}";
ObjectMapper objectMapper = new ObjectMapper();
Product product = objectMapper.readValue(s, Product.class);
System.out.println(product.getTimestamp());
}
class Product {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private Timestamp timestamp;
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}
Here是文档。
更新
对于Stock
,您需要将其作为嵌套对象传递以遵守Product
类结构,例如:
{
"id": "Product ID",
"timestamp": "2017-07-16 22:54:01.754",
"stock" : {
"id": "Stock ID",
"timestamp": "2000-07-16 22:54:01.754",
"quantity": "250"
}
}
你的curl命令将是:
$ curl -i -X POST -H "Content-Type:application/json" -d "{ \"id\": \"Product ID\",
\"timestamp\": \"2017-07-16 22:54:01.754\", \"stock\" : { \"id\": \"Stock ID\",
\"timestamp\": \"2000-07-16 22:54:01.754\", \"quantity\": \"250\" }}"
http://localhost:8080/api/v1/products/createProduct