Can not construct instance of java.sql.Timestamp from String value

Keywords: JSON, Jersey, Java, Microservices, Solution, Fix

You're retrieving a JSON Object that contains a Java Timestamp datatype and when converting the object on the client you see something like the following:

Can not construct instance of java.sql.Timestamp from String value '01-18-2018 09:16:32': not a valid representation (error: Failed to parse Date value '01-18-2018 09:16:32': Can not parse date "01-18-2018 09:16:32": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

This post provides one possible solution. Hint: It involves field annotations in the Client object.

Original Object

We're passing an object called JsonPost from our Microservice with a postDate Timestamp field annotated the way we like it.

The JSON postDate would look like this.

On the Client

We retrieve the JsonPost object in a Web Client. As is the case in many Distributed Applications, the Java Object on the client is a different Class object than the one sent, in this case a simplified object called Post. Here's an excerpt from the Post class where you'll see our postDate Timestamp field.

Remember our solution hint involving JSON Annotations in the Client object? Yeap, our Client postDate field needs to match the Microservice object postDate properties. Thus the fix is to add a @JsonFormat annotation to postDate in our Web Client object.