Lightadmin for timestamp fields such as:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="started_at")
Date startedAt;
does not format them but shows them as the number of milliseconds since the epoch, e.g. 1398940456150
.
When you enter a Lightadmin edit page e.g. http://localhost:8080/admin/domain/user/1/edit
the values which the form is actually populated with are received in another request - http://localhost:8080/admin/rest/user/1/unit/formView?_=1401699535260
, which returns JSON with:
...
"startedAt" : {
"name" : "startedAt",
"title" : "started at timestamp",
"value" : 1398940456150,
"type" : "DATE",
"persistable" : true,
"primaryKey" : false
}
...
The task is to change 1398940456150
to e.g. 01.05.2014 10:34:16
.
According to my investigation, org.lightadmin.core.rest.DynamicRepositoryRestController.entity()
is the entry point of such requests, the code that is responsible for generating JSON is 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
is an instance of org.codehaus.jackson.map.ObjectMapper.ObjectMapper
, initialized with defaults. If it were possible to add these two lines:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.getSerializationConfig().setDateFormat(df);
it would do the job, the question is how this can be done?
I posted this fix on Github - but here it is:
I fixed this issue by changing the class DomainTypeResourceModule in the lightadmin code. Here is the updated source code of the class. There may be a better way to fix it - but this was the least intrusive way and it covered both, serialize and deserialize.