1

I have a Java Spring REST API that receives a JSON string in the body of a POST request. JSON string is large; it contains multiple nested objects.

Upon receiving the POST request, I am converting the string into a DTO object.

Problem

One of the nested object contains a key startDate that is not being de-serialized correctly.

Value of the startDate object is:

"startDate": 1622746800000

I have tried de-serializing the string using Jackson as well as Gson BUT both of them fail to parse the startDate correctly.

Exception thrown when using Gson:

java.text.ParseException: Unparseable date: "1622746800000"

If I pass the startDate as null, everything works fine - string is converted into DTO object correctly.

Question

Any idea what could be a possible cause for this problem? How can I fix it?


P.S. Value for startDate is received as part of a response from the backend. I am sending that same response string to the backend in the POST request body.


Code:

Following is the code that parses the string received in the request's body:

ObjectMapper mapper = new ObjectMapper();
MyDTOClass evData = mapper.convertValue(evDataJsonStr, MyDTOClass.class);

I have tried with Gson as well:

Gson mapper = new Gson();
MyDTOClass evData = mapper.fromJson(evDataJsonStr, MyDTOClass.class);

This question is similar but didn't help in my case.

Following image shows the format of startDate as shown in the Intellij IDEA's expression evaluator:

enter image description here

Above value when serialized becomes: 1622746800000 and this is what i am trying to de-serialize.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • How do you parse the date? I assume that that number is the epoch time in milliseconds. Maybe your parser is expecting a specific date format. With the old Java ```Date``` API it would simply be ```new Date(1622746800000)```. Since it is a number and not a String nor parsing should be involved at all. – geanakuch Aug 30 '21 at 11:15
  • What is the format of your date? You can use `com.fasterxml.jackson.annotation.JsonFormat` annotation to set the expected format. – akuma8 Aug 30 '21 at 11:20
  • @geanakuch added the code i am using to parse the string received from the frontend. – Yousaf Aug 30 '21 at 11:24
  • @akuma8 added an image that shows the format of `startDate`. – Yousaf Aug 30 '21 at 11:24
  • Have a look here https://www.baeldung.com/jackson-serialize-dates – akuma8 Aug 30 '21 at 11:30
  • Did you solve your issue? – akuma8 Aug 30 '21 at 14:49
  • @akuma8 yes I did. Thank you for sharing the link; I had already read that but the answer below is what I used to solve this problem. – Yousaf Aug 31 '21 at 05:02

1 Answers1

1

In my understanding Gson cannot de-serialize epoch time by default. But Jackson can.

For Jackson you can do the following,

ObjectMapper mapper = new ObjectMapper();
MyDTOClass evData = mapper.readValue(evDataJsonStr, MyDTOClass.class);

Please note that its ObjectMapper's readValue method not convertValue method

ray
  • 1,512
  • 4
  • 11