gpt4 book ai didi

java - 正确执行cURL请求

转载 作者:行者123 更新时间:2023-12-02 10:15:40 25 4
gpt4 key购买 nike

下面提供了 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\":\"产品 ID\",\"时间戳\":\"2017-07-16 22:54:01.754\",\"id\": \"股票 ID\",\"时间戳\":\"2000-07-16 22:54:01.754\",\"数量\": \"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"}

但是,数据库条目不正确,

enter image description here enter image description here

如何正确编写 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字段没有被反序列化,您需要使用@JsonFormat注释Timestamp字段,例如:

@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

关于java - 正确执行cURL请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com