Serializing Joda DateTime with Jackson and Spring -
i having problems consistently serializing , deserializing joda datetime java json , again using spring boot , jackson-databind 2.5.2. pom.xml looks 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 serialize datetime object integer representing datetime. not expected actually, fine. when go save object following error...
failed convert property value of type 'java.lang.string' required type 'org.joda.time.datetime' property 'endtime'; nested exception org.springframework.core.convert.conversionfailedexception: failed convert type java.lang.string type org.joda.time.datetime value '1428600998511'
for reason serializing integer deserializing if it's string, not. tried setting endtime = new date(intvalue) before calling rest service , failed trying convert string 'tue apr 28 2015 00:00:00 gmt-0700 (pdt)' datetime.
what doing wrong?
update:
here json and try post right 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 }
for more re-usable mechanism, can create jsonserializer
:
/** * when passing json around, it's use standard text representation of * date, rather full details of joda datetime object. therefore, * serialize value iso-8601 standard: * <pre>yyyy-mm-dd't'hh:mm:ss.sssz</pre> * can parsed javascript library such 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 can annotate get
methods with:
@jsonserialize(using = jsonjodadatetimeserializer.class)
this gives consistent formatting throughout application, without repeating text patterns everywhere. it's timezone aware.
Comments
Post a Comment