Arefe
Arefe

Reputation: 12397

Execute the cURL request properly

I have 2 entities provided below:

@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;
}

My intention is to insert a product object in the database, so, if later I use the GET command, I will be able to retrieve a JSON similar to:

{
   "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"

   } 
}

The API for the POST call is provided below:

@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);
    }
}

The cURL request is provided,

$ 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

The command is successful and delivers the output,

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"}

However, the database entries are not correct,

enter image description here enter image description here

How do I write the cURL POST request properly?

With the CURL command, I would like to populate the tables with the data and the response should return the same,

{ "productId": "Product ID" "requestTimestamp": "2017-07-16 22:54:01.754"

"stock": {

 "id": "Stock ID", 
 "timestamp": "2000-07-16 22:54:01.754",
 "quantity": "250"

} }

Upvotes: 0

Views: 626

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30809

Looks like the Timestamp field is not getting deserialised, you need to annotate Timestamp field with @JsonFormat, e.g.:

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")

Below is an example:

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 is the documentation.

update

For the Stock, you need to pass it as a nested object to adhere to Product class structure, e.g.:

{
  "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"
  }
}

Your curl command will be:

$ 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

Upvotes: 1

Related Questions