java - Change Jackson date formatting setting in spring data rest webmvc -


lightadmin timestamp fields such as:

@temporal(temporaltype.timestamp) @column(name="started_at") date startedat; 

does not format them shows them number of milliseconds since epoch, e.g. 1398940456150.

when enter lightadmin edit page e.g. http://localhost:8080/admin/domain/user/1/edit values form populated received in request - http://localhost:8080/admin/rest/user/1/unit/formview?_=1401699535260, returns json with:

... "startedat" : {     "name" : "startedat",     "title" : "started @ timestamp",     "value" : 1398940456150,     "type" : "date",     "persistable" : true,     "primarykey" : false } ... 

the task change 1398940456150 e.g. 01.05.2014 10:34:16.

according investigation, org.lightadmin.core.rest.dynamicrepositoryrestcontroller.entity() entry point of such requests, code responsible generating json inside: org.springframework.data.rest.webmvc.repositoryawaremappinghttpmessageconverter.writeinternal():

try {   mapper.writevalue(jsongenerator, object); } catch(ioexception ex) {   throw new httpmessagenotwritableexception("could not write json: " + ex.getmessage(), ex); } 

mapper instance of org.codehaus.jackson.map.objectmapper.objectmapper, initialized defaults. if possible add these 2 lines:

simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); mapper.getserializationconfig().setdateformat(df); 

it job, question how can done?

i posted fix on github - here is:

i fixed issue changing class domaintyperesourcemodule in lightadmin code. here updated source code of class. there may better way fix - least intrusive way , covered both, serialize , deserialize.

package org.lightadmin.core.rest;  import java.io.ioexception; import java.text.parseexception; import java.text.simpledateformat; import java.util.date;  import org.codehaus.jackson.jsongenerator; import org.codehaus.jackson.jsonparser; import org.codehaus.jackson.jsonprocessingexception; import org.codehaus.jackson.version; import org.codehaus.jackson.map.deserializationcontext; import org.codehaus.jackson.map.serializerprovider; import org.codehaus.jackson.map.deser.std.stddeserializer; import org.codehaus.jackson.map.module.simpledeserializers; import org.codehaus.jackson.map.module.simplemodule; import org.codehaus.jackson.map.module.simpleserializers; import org.codehaus.jackson.map.ser.std.serializerbase; import org.springframework.hateoas.resource;  public class domaintyperesourcemodule extends simplemodule {      private final domaintypetoresourceconverter domaintypetoresourceconverter;      public domaintyperesourcemodule(final domaintypetoresourceconverter domaintypetoresourceconverter) {         super("domaintyperesourcemodule", version.unknownversion());          this.domaintypetoresourceconverter = domaintypetoresourceconverter;     }      @override     public void setupmodule(final setupcontext context) {         simpleserializers serializers = new simpleserializers();         serializers.addserializer(domaintyperesource.class, new domaintyperesourceserializer());         serializers.addserializer(date.class, new jsondateserializer());          simpledeserializers deserializers = new simpledeserializers();         deserializers.adddeserializer(date.class, new jsondatedeserializer());          context.adddeserializers(deserializers);         context.addserializers(serializers);     }      private class domaintyperesourceserializer extends serializerbase<domaintyperesource> {          protected domaintyperesourceserializer() {             super(domaintyperesource.class);         }          @override         public void serialize(domaintyperesource value, jsongenerator jgen, serializerprovider provider) throws ioexception {             if (null == value) {                 provider.defaultserializenull(jgen);                 return;             }              final resource resource = domaintypetoresourceconverter.convert(value.getresource(), value.getconfigurationunittype(), value.getfieldmetadatas());              jgen.writeobject(resource);         }     }      private class jsondateserializer extends serializerbase<date> {          protected jsondateserializer() {             super(date.class);         }          @override         public void serialize(date date, jsongenerator gen, serializerprovider provider) throws ioexception, jsonprocessingexception {              simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");             string formatteddate = date == null ? "" : dateformat.format(date);              gen.writestring(formatteddate);         }      }      private class jsondatedeserializer extends stddeserializer<date> {          protected jsondatedeserializer() {             super(date.class);         }          @override         public date deserialize(jsonparser json, deserializationcontext context) throws ioexception, jsonprocessingexception {              try {                 if(json.gettext() != null && !"".equals(json.gettext().trim())) {                     try {                         return new date(long.parselong(json.gettext()));                     }                     catch(numberformatexception nex){                         simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");                         return dateformat.parse(json.gettext());                     }                 }                 else return null;             }             catch (parseexception e){                 return null;             }         }      }  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -