If I get the following json from a RESTful client, how do I elegantly unmarshal the java.util.Date? (Is it possible without providing (aka. hard-coding) the format, that's what I mean by elegantly...)
{
"class": "url",
"link": "http://www.empa.ch",
"rating": 5,
"lastcrawl" : "2009-06-04 16:53:26.706 CEST",
"checksum" : "837261836712xxxkfjhds",
}
Be aware that the new version of Grails 2.3+ supports this type of feature out of the box. See Date Formats for Data Binding
With that said, if you are forced to use a version of Grails prior to 2.3, the
CustomEditorRegistrar
can be updated using the following code to eliminate the deprecation warning, and also uses the@Component
annotation, which allows you to remove / skip the step of adding the bean directly inresources.groovy
. Also not that I changed the grails configuration property name to grails.databinding.dateFormats, which matches the property now supported in Grails 2.3+. Finally, my version is a .groovy, not .java file.The cleanest way is probably to register a custom DataBinder for possible date formats.
You'd also need to implement a PropertyEditorRegistrar
and create a Spring-bean definition in your grails-app/conf/spring/resources.groovy:
and finally define the date formats in your grails-app/conf/Config.groovy: