0

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?

Ashish K Agarwal
  • 1,172
  • 3
  • 17
  • 33
  • Thanks @AlexSavitsky for your reply. How do I include this module? I am using maven – Ashish K Agarwal Mar 05 '18 at 17:43
  • Generally, googling "maven " would point you right to its maven central page, if it exists. For this one, it's https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-modules-java8/2.9.4. This is the latest version, but use the one that corresponds to your main Jackson version, for best results. Copy the `` section and put it in your Maven POM – Alex Savitsky Mar 05 '18 at 17:46
  • This worked. Thanks @AlexSavitsky – Ashish K Agarwal Mar 05 '18 at 17:47

0 Answers0