Serializing Joda DateTime with Jackson and Spring

I am having problems serializing and deserializing Joda DateTime from java to json and back using Spring Boot and Jackson-databind 2.5.2. My pom.xml looks like this.

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-joda</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.1.RELEASE</version> </dependency> 

When I serialize a DateTime object, I get an integer representing DateTime. Not what I really expected, but fine. BUT, when I go to save my object back, I get the following error ...

 Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type org.joda.time.DateTime for value '1428600998511' 

For some reason, he serializes it to an integer, but then deserializes it as if it is a string that it is not. I also tried setting endTime = a new date (intValue) before calling the rest service, and also did not try to convert a string like "Tue Apr 28 2015 00:00:00 GMT-0700 (PDT)" to DateTime.

What am I doing wrong?

UPDATE:

Here is the JSON in which I am GET and that I am trying to send the POST immediately back.

 { id: 4, username: "", name: "eau", email: "aoue", verbatimLocation: null, latitude: null, longitude: null, startTime:null, endTime: 1429034332312, description: "ueoa", media: [ ], timeSubmitted: 1428600998000, status: null, submissionid: null } 
+5
source share
2 answers

For more reusability, you can create a JsonSerializer :

 /** * When passing JSON around, it good to use a standard text representation of * the date, rather than the full details of a Joda DateTime object. Therefore, * this will serialize the value to the ISO-8601 standard: * <pre>yyyy-MM-dd'T'HH:mm:ss.SSSZ</pre> * This can then be parsed by a JavaScript library such as moment.js. */ public class JsonJodaDateTimeSerializer extends JsonSerializer<DateTime> { private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime(); @Override public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.print(value)); } } 

Then you can annotate your get methods with:

 @JsonSerialize(using = JsonJodaDateTimeSerializer.class) 

This gives you consistent formatting throughout the application without repeating text patterns around the world. He also knows about time zones.

+8
source

In the end, I was able to make beerbajay and use ...

 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSZ") 

... to serialize my date. However, I will return to using Long instead of DateTime, because dealing with a date on the javascript side was too troublesome. Finding a template that worked for jquery datepicker, joda DateTime, and for postgresql turned out to be too much work for the little time I had.

+2
source

All Articles