2011-12-19 49 views
2

我無法爲Grails使用JodaTime插件。該插件似乎正確地轉換爲JSON的輸出,但似乎無法再接受輸出的日期格式作爲輸入時,當相同的JSON對象被送回。Grails無法將日期/時間從JSON解析回約達日期時間

這些是我得到的錯誤:

Field error in object 'server.Session' on field 'lastUpdated': rejected value [2011-12-19T14:15:03-06:00]; codes [typeMismatch.server.Session.lastUpdated,typeMismatch.lastUpdated,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [server.Session.lastUpdated,lastUpdated]; arguments []; default message [lastUpdated]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'lastUpdated'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2011-12-19T14:15:03-06:00" is malformed at "11-12-19T14:15:03-06:00"] 
Field error in object 'server.Session' on field 'dateCreated': rejected value [2011-12-19T14:15:03-06:00]; codes [typeMismatch.server.Session.dateCreated,typeMismatch.dateCreated,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [server.Session.dateCreated,dateCreated]; arguments []; default message [dateCreated]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'dateCreated'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2011-12-19T14:15:03-06:00" is malformed at "11-12-19T14:15:03-06:00"] id=33 version=0> 

這裏是非常基本的域模型:

package server 

import org.joda.time.DateTime 

class Session { 

    DateTime dateCreated 
    DateTime lastUpdated 
    String ip 

    static constraints = { 
     ip blank: false 
    } 
} 

這裏是展示和更新方法(分別發送和接收JSON):

def show() { 
    def sessionInstance = Session.get(params.id) 
    if (!sessionInstance) { 
     flash.message = message(code: 'default.not.found.message', args: [message(code: 'session.label', default: 'Session'), params.id]) 
     redirect(action: "list") 
     return 
    } 

    def response = [sessionInstance: sessionInstance] 
    withFormat { 
     html response 
     json {render response as JSON} 
     xml {render response as XML} 
    } 
} 

def update() { 

    def sessionInstance = Session.get(params.id) 
    if (!sessionInstance) { 
     flash.message = message(code: 'default.not.found.message', args: [message(code: 'session.label', default: 'Session'), params.id]) 
     redirect(action: "list") 
     return 
    } 

    if (params.version) { 
     def version = params.version.toLong() 
     if (sessionInstance.version > version) { 
      sessionInstance.errors.rejectValue("version", "default.optimistic.locking.failure", 
        [message(code: 'session.label', default: 'Session')] as Object[], 
        "Another user has updated this Session while you were editing") 
      render(view: "edit", model: [sessionInstance: sessionInstance]) 
      return 
     } 
    } 

    sessionInstance.properties = params // <----- This is what causes the errors 

    log.info("instance: ${sessionInstance.dump()}") // <------ This is where I'm seeing the error messages 

    if (!sessionInstance.save(flush: true)) { 
     render(view: "edit", model: [sessionInstance: sessionInstance]) 
     return 
    } 

    flash.message = message(code: 'default.updated.message', args: [message(code: 'session.label', default: 'Session'), sessionInstance.id]) 
    redirect(action: "show", id: sessionInstance.id) 
} 

我不知道我需要做什麼來糾正這個問題,或者即使我正確地做事,因爲我對Grails很陌生。在所有現實中,導致問題的兩個日期字段應該由GORM在內部100%處理,並且我寧願控制器完全忽略它們,但是會有其他類似的日期字段,當域模型獲取時需要更新填入。

我怎樣才能得到自動的JSON反編組,正確地轉換回喬達時間DateTime對象?

注意:這是目前客戶端 - 服務器應用程序的概念驗證。

回答

3

我不確定原因,也不知道此修補程序的工作原理,但將jodatime.format.html5 = true添加到Config.groovy文件會使一切正常。

據我所知,JSON輸出沒有變化,但無論出於何種原因,它都會使JSON輸入處理和數據綁定起作用。

即使暗示這一點的文檔的唯一外表是here

試圖通過諸如jodatime.format.org.joda.time.DateTime = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"之類的東西來設置DateTime的格式根本沒有任何效果。