正确执行cURL请求

时间:2019-02-15 09:03:05

标签: java spring-boot http 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"

   } 
}

下面提供了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"}

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

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"

} }

1 个答案:

答案 0 :(得分:1)

好像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