Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

likhia avatar image
likhia asked likhia commented

Cannot retrieve timestamp column with Spring Boot REST API

hi all,

I'm trying the REST API using Spring Boot. I have the error below when mapping returned object to Java Pojo class. I know it is due to the "created" attribute which is java.util.Date and timestamp in cassandar.

I have tried with java.time.Instant but getting the same error. Can anyone help to advise?

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [class com.example.demo.services.Data] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.Date` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (PushbackInputStream); line: 1, column: 144] (through reference chain: com.example.demo.services.Data["data"]->java.util.ArrayList[0]->com.example.demo.services.Product["created"])] with root cause
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.Date` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (PushbackInputStream); line: 1, column: 144] (through reference chain: com.example.demo.services.Data["data"]->java.util.ArrayList[0]->com.example.demo.services.Product["created"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1420) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.DeserializationContext.extractScalarFromObject(DeserializationContext.java:932) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:1198) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:201) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:303) ~[jackson-databind-2.13.1.jar:2.13.1]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:281) ~[jackson-databind-2.13.1.jar:2.13.1]

Thanks

java driverspring-boot
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered likhia commented

The CQL timestamp type looks like a date but is in fact stored as the number of milliseconds since Unix epoch.

You will need to map it to a java.time.Instant type then use the Instant.atZone() method to convert it to a date.

I don't have a code example so you'll need to consult the Spring framework docs on Data Mapping and Type Conversion. Cheers!

2 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

[Not an answer]

0 Likes 0 ·

Thanks Erick. I have tried with java.time.Instant and java.time.LocalDateTime but still getting same error.

The response of the Datastax REST API is mapped to the Data object which contains a Product object containing the LocalDateTime attribute for the timestamp column.

Below is the code of my service.

RestTemplate restTemplate = new RestTemplate();
String baseUrl = getBaseURL() + id; //+ "?fields=description%2C%20productname%2Cid%2Cprice"; 
URI uri = new URI(baseUrl); 

HttpHeaders headers = new HttpHeaders();
headers.set("x-cassandra-token", ASTRA_DB_APPLICATION_TOKEN);

HttpEntity<Data> requestEntity = new HttpEntity<>(null, headers); 
ResponseEntity<Data> result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, Data.class); 
return result.getBody();

Can you help to advise what can be wrong?

Thanks

0 Likes 0 ·