Search code examples
grailsdata-bindinggrails-ormgrails-2.0

Grails GORM date databinding


I am using Grails 2.3 and I have a domain with a date field:

class DomainClass {
    String title
    Date datetime
} 

And I have a json coming in to the RestfulController with timestamp in milliseconds:

{"title": "name", "datetime": 957044075522}

The controller is binding the title correctly but for the datetime field, it is actually an error:

Could not find matching constructor for: java.util.Date(com.google.gson.internal.LazilyParsedNumber)

What's the best way to fix this?

Thanks.


Solution

  • Both of the Brain F and zoran119 answers make sense so I voted them up. If anyone is doing the same thing, this is the actual answer that works:

    @BindUsing({ obj, source -> source['datetime']?.longValue() })
    Date datetime