2

Trying to use the java.time API in Grails 3.1.x rest controllers.

My domain object "Absence" has a field

LocalDate date

build.gradle contains

compile "org.jadira.usertype:usertype.extended:5.0.0.GA"

as a dependency

and application.groovy contains

grails.gorm.default.mapping = {
'user-type'(type: org.jadira.usertype.dateandtime.threeten.PersistentLocalDate, class: LocalDate)
'user-type'(type: org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime, class: LocalDateTime)

}

This works for persisting the domain object, so the backend/db side of it seems to work as expected.

However, when serializing the domain object through a RestController, I am unable to properly formatting the way I want (i.e. yyyy-MM-dd). The output is

"date": {
        "chronology": {
            "calendarType": "iso8601", 
            "class": "java.time.chrono.IsoChronology", 
            "id": "ISO"
        }, 
        "class": "java.time.LocalDate", 
        "dayOfMonth": 19, 
        "dayOfWeek": {
            "enumType": "java.time.DayOfWeek", 
            "name": "THURSDAY"
        }, 
        "dayOfYear": 140, 
        "era": {
            "enumType": "java.time.chrono.IsoEra", 
            "name": "CE"
        }, 
        "leapYear": true, 
        "month": {
            "enumType": "java.time.Month", 
            "name": "MAY"
        }, 
        "monthValue": 5, 
        "year": 2016
    }, 

but what I really want is

"date": "2016-05-19"

I have tried to find the solution in the Grails documentation, however, even after searching for the answer multiple times I can't seem to figure it out. I have found documentation on formatting java.util.Date, however this is not what I want.

The "problem" is not very much with the GET request, but for POST and PUT requests it is not practical, so I need to get the short-format yyyy-MM-dd working.

Has anyone found a solution to this?

runeaen
  • 461
  • 5
  • 22

1 Answers1

0

Got an answer from the Grails Slack question channel. It is not easily done at the moment. You need to customize both data binding and json rendering to accomplish this, and ultimately I gave up

runeaen
  • 461
  • 5
  • 22