I am working on a project using Spring Boot (latest version) where I have created 1 webservice whose end point is - /transaction. While invoking the /transaction service via POST i am getting the exception as below -
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"])
Following is my class Transaction -
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();
}
}
I am using java 8's Instant class for timestamp. From the user I will get POST request as -
{
"amount" : 12.3,
"timestamp": 1478192204000
}
The data types of amount and timestamp are double and long respectively. I used Instant class because I am doing a lot of manipulation using the Duration class of Java 8.
Any pointers how to resolve this issue?