Grails无法从JSON解组日期/时间回到joda DateTime [英] Grails unable to unmarshall date/time from JSON back into joda DateTime

查看:125
本文介绍了Grails无法从JSON解组日期/时间回到joda DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将JodaTime插件用于Grails时遇到麻烦.该插件似乎已正确转换为输出的JSON,但是当发送回相同的JSON对象时,它似乎无法再次接受输出的日期格式作为输入.

I'm having trouble using the JodaTime plugin for Grails. The plugin seems to be correctly converting to JSON for the output, but it does not seem to be able to accept the outputted date format again as input when the same JSON object is sent back in.

这些是我得到的错误:

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
    }
}

这是show和update方法(分别发送和接收JSON):

And here are the show and update methods (send and receive JSON respectively):

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%处理,我宁愿控制器完全忽略它们,但是一旦域模型获得,其他类似的日期字段将需要更新填写.

I'm not sure what I need to do to correct this, or even if I'm doing things correctly, since I'm very new to Grails. In all reality, the two date fields that are causing issues should be handled 100% internally by GORM and I would rather the controller ignore them completely, but there will be other date fields like them that will need to be updated once the domain model gets filled in.

如何获取自动JSON编组以正确地转换回joda时间DateTime对象?

How can I get the automatic JSON unmarshalling to correctly convert back into joda time DateTime objects?

注意:这目前是客户端服务器应用程序的概念证明.

Note: This is currently a proof-of-concept for a client-server application.

推荐答案

我不确定原因,也无法确定此修复程序为何起作用,但是不确定jodatime.format.html5 = true到Config.groovy文件中是否可以使一切正常.

I'm not sure of the cause, nor why this fix works, but adding jodatime.format.html5 = true to the Config.groovy file makes everything work.

据我所知,JSON输出没有任何变化,但是由于某种原因,它使JSON输入处理和数据绑定工作正常.

As far as I can tell, there is no change in the JSON output, but for whatever reason, it makes the JSON input handling and data binding work.

甚至暗示了这一点的唯一文献是

The only semblance of documentation that even hints at this is here.

尝试通过诸如jodatime.format.org.joda.time.DateTime = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"之类的方法设置DateTime的格式完全没有效果.

Trying to set the format for DateTime via something like jodatime.format.org.joda.time.DateTime = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ" had no effect at all.

这篇关于Grails无法从JSON解组日期/时间回到joda DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆