无法从字符串反序列化类型为java.sql.Timestamp的值

时间:2019-03-03 16:08:24

标签: java jackson

我在下面提供了我要工作的实体

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

我想将一些数据发布到存储中,并为此目的使用cURL。

我的cURL命令是

$ 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

我收到错误消息,

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

我该如何纠正?

这肯定是时间戳记,就像我之前使用@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")尝试的那样,并且运行良好。因此,该应用程序的另一部分很好,我只需要正确的cURL请求即可。

1 个答案:

答案 0 :(得分:0)

我不得不将定义更改为:

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

cURL调用将是:

$ 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

数据库存储:

enter image description here