java - 正确执行 cURL 请求
问题描述
我在下面提供了 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"
}
}
调用的 APIPOST
如下所示:
@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;
}
}
这是文档。
更新
对于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
推荐阅读
- python - 在 Python 中发布一个二维数组
- model - 如何从头开始为训练模型制作自定义名称
- facebook - 使用 Facebook Graph API 从见解节点中提取“值”字段
- mysql - 选择相同外键行的多个事务导致锁定等待超时
- sql-server - 将 SQL SERVER MANAGEMENT STUDIO 与 CPANEL Laravel 连接起来
- python - 将 KML 文件转换为 CSV 并在 rgdal 和 sf 包以及 Python 中出现错误
- python - python,列表和生成器连接
- android - 不显示底部表
- scala - 需要两个隐式参数之一的 Scala 方法
- c# - 通过实体框架 6.4 连接到 linux mssql 服务器