我正在使用Spring Boot(最新版本)开发一个项目,我创建了1个webservice,其终点是 - / transaction 。在通过POST调用/ transaction服务时,我得到如下例外 -
WARN 7864 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of java.time.Instant: no long/Long-argument constructor/factory method to deserialize from Number value (1478192204000); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.Instant: no long/Long-argument constructor/factory method to deserialize from Number value (1478192204000)
at [Source: java.io.PushbackInputStream@75d6c7; line: 4, column: 15] (through reference chain: com.demo.statsservice.statsservice.models.Transaction["timestamp"])
以下是我的班级交易 -
import java.time.Instant;
public class Transaction {
private double amount;
private Instant timestamp;
public Transaction() {}
public Transaction(double amount, Instant timestamp) {
this.amount = amount;
this.timestamp = timestamp;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Instant getTimestamp() {
return timestamp;
}
public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
我正在使用java 8的Instant类来获取时间戳。用户将收到POST请求 -
{
"amount" : 12.3,
"timestamp": 1478192204000
}
金额和时间戳的数据类型分别为double和long。我使用了Instant类,因为我使用Java 8的Duration类进行了大量的操作。
如何解决此问题?