Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot deserialize value of type `java.sql.Timestamp` from String

Tags:

java

jackson

I have the entities that I work provided below,

@Entity
public class Product {


    @Id
    @Column(name = "productId")
    private String productId;

    @Column(name = "requestTimestamp")
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone="Europe/Berlin")
    private Timestamp requestTimestamp;

    @Embedded
    private Stock stock;

    public Product() {

    }

    public Product(String productId, Timestamp requestTimestamp, Stock stock) {
        this.productId = productId;
        this.requestTimestamp = requestTimestamp;
        this.stock = stock;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public Timestamp getRequestTimestamp() {
        return requestTimestamp;
    }

    public void setRequestTimestamp(Timestamp requestTimestamp) {
        this.requestTimestamp = requestTimestamp;
    }

    public Stock getStock() {
        return stock;
    }

    public void setStock(Stock stock) {
        this.stock = stock;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product)) return false;
        Product product = (Product) o;
        return getProductId().equals(product.getProductId()) &&
                getRequestTimestamp().equals(product.getRequestTimestamp()) &&
                getStock().equals(product.getStock());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getProductId(), getRequestTimestamp(), getStock());
    }

    @Override
    public String toString() {
        return "Product{" +
                "productId='" + productId + '\'' +
                ", requestTimestamp=" + requestTimestamp +
                ", stock=" + stock +
                '}';
    }
}


@Embeddable
public class Stock {

    @Column(name = "id")
    private String id;

    @Column(name = "timestamp")
    // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone="Europe/Berlin")
    private Timestamp timestamp;

    @Column(name = "quantity")
    private int quantity;

    public Stock() {

    }

    public Stock(String id, Timestamp timestamp, int quantity) {

        this.id = id;
        this.timestamp = timestamp;
        this.quantity = quantity;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Stock)) return false;
        Stock stock = (Stock) o;
        return getQuantity() == stock.getQuantity() &&
                getId().equals(stock.getId()) &&
                getTimestamp().equals(stock.getTimestamp());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getTimestamp(), getQuantity());
    }

    @Override
    public String toString() {
        return "Stock{" +
                "id='" + id + '\'' +
                ", timestamp=" + timestamp +
                ", quantity=" + quantity +
                '}';
    }
}

I would like to POST a few data's to the storage and use cURL for the purpose.

My cURL commmands are,

$ curl -i -X POST -H "Content-Type:application/json" -d "{  \"productId\": \"Apple ID\", \"requestTimestamp\": \"2017-07-16'T'22:54:01.754Z\",  \"stock\" : {  \"id\": \"Apple ID\", \"timestamp\": \"2000-07-16 22:54:01.754\",  \"quantity\": \"250\"  }}" http://localhost:8080/api/v1/products/createProduct

I get the error message,

{"timestamp":"2019-03-03T15:53:16.421+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize value of type `java.sql.Timestamp` from String \"2017-07-16'T'22:54:01.754Z\": expected format \"yyyy-MM-dd'T'HH:mm:ss.SSSZ\"; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.sql.Timestamp` from String \"2017-07-16'T'22:54:01.754Z\": expected format \"yyyy-MM-dd'T'HH:mm:ss.SSSZ\"\n at [Source: (PushbackInputStream)

How do I correct this?

This surely with the Timestamp as I tried earlier with the @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") and worked fine. So, the other part of the app is good, I just need the right temrs for the cURL request.

like image 633
Heisenberg Avatar asked Sep 10 '25 16:09

Heisenberg


1 Answers

I had to change the definition to something like:

@Column(name = "requestTimestamp")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="Europe/Berlin")
private Timestamp requestTimestamp;

The cURL call will be:

$ curl -i -X POST -H "Content-Type:application/json" -d "{  \"productId\": \"Product ID\", \"requestTimestamp\": \"2017-07-16T22:54:01.754Z\",  \"stock\" : {  \"id\": \"Stock ID\", \"timestamp\": \"2000-07-16T22:54:01.754Z\",  \"quantity\": \"250\"  }}" http://localhost:8080/api/v1/products/createProduct

The database storage:

enter image description here

like image 148
Heisenberg Avatar answered Sep 13 '25 05:09

Heisenberg