我正在编写一个 Grail REST 服务,并且我已经定义了自定义 JSON 转换器。例如,我有一个 event
每当客户端请求这样的对象时,都会像这样转换的类...
// in src/groovy/EventMarshaller.groovy
class EventMarshaller {
void register(Object config) {
config.registerObjectMarshaller(Event) { Event e ->
return [
id: e.id,
title: e.title,
description: e.description,
dateCreated: e.dateCreated.format('yyyy-MM-dd'),
creator: e.creator
]
}
}
我注册了
EventMarshaller
在 CustomObjectMarshaller
内预计 named config
参数,以便可以容纳不同的 REST API 版本...// in src/groovy/CustomObjectMarshallers.groovy
def register(String str) {
JSON.createNamedConfig(str) { cfg ->
marshallers.each {
it.register(cfg);
}
}
}
// in BootStrap.groovy init section...
def springContext = WebApplicationContextUtils.getWebApplicationContext( servletContext );
springContext.getBean("customObjectMarshallers").register("v1");
每当我调用
GET
时,这就像一个魅力。一个 event
通过 REST API 对象,域对象被转换为指定的 JSON 格式。例如,我的 event
Controller 有一个 index
行动...def index(String v)
{
def configName = 'v' + (v ?: 1)
def listOfEvents = Event.list()
// why does this only work when converting domain object to json?
JSON.use(configName) {
respond listOfEvents
}
}
现在我需要
update
和 save
PUT
时的操作和 POST
从客户端接收命令。到目前为止,我的 update
中有以下内容行动...def update(Long id, String v)
{
def configName = 'v' + (v ?: 1)
// how do I specify the version? JSON.use(configName) doesn't seem to work?
def jsonobj = JSON.parse(request);
def newEvent = new Event(jsonobj);
def evRequest = new EventRequest(jsonobj)
evRequest.errors.allErrors.each {
println it
}
...
谁能解释我如何告诉
update action
将 JSON 转换为域对象时使用哪个配置? (我根本没有在网上看到任何这样的例子。)另外,dateCreated
newEvent
中的字段解析 JSON 后 object 为 null。我如何确保如果 dateCreated
字段存在,它将被解析为其原始 date
目的?这是代码中引用的命令对象...
/**------------------------------------------------------------
// EVENT REQUEST COMMAND OBJECT
//----------------------------------------------------------**/
class EventRequest
{
String id;
String title;
String description;
byte[] photo;
@BindingFormat('yyyy-MM-dd')
Date dateCreated;
//------------------------------------------------------------
// IMPORT CONTRAINTS FROM EVENT DOMAIN MODEL
//------------------------------------------------------------
static constraints = {
importFrom Event;
}
}
和
event
它映射到的域类...import java.util.Date;
import org.emvigr.user.*
import org.grails.databinding.BindingFormat;
class Event {
String title
String description
byte[] photo
@BindingFormat('yyyy-MM-dd')
Date dateCreated
// we can call user.addToEvents
static belongsTo = [
creator : User
]
static hasMany = [
portals : Portal
]
static constraints = {
title maxSize: 50
description nullable: true, maxSize: 300
photo nullable: true, maxSize: 2 * 1024 * 1024
dateCreated nullable: true
portals nullable: true
}
// when Events are accessed sort by the dateCreated (descending)
static mapping = {
sort dateCreated:"desc"
}
}
非常感谢任何帮助!
最佳答案
感谢 Joshua Moore 提供有关命名空间和 dmahapatro 重新日期绑定(bind)的信息。我在解析 update
中的日期参数时也遇到了问题。我现在意识到这是因为我(错误)使用命令对象的方式。
对于遇到相同问题的任何人,这可能是 2.4.4 特有的,但是根据 official docs,类仅在用作操作的参数时才被视为命令对象。 .一旦我像这样修改了更新操作......
def update(EventRequestCommand cmd)
{
cmd.errors.allErrors.each {
println it
}
if (!cmd.hasErrors()) {
def ev = Event.get(cmd.id);
// save changes
}
else {
// handle error
}
}
可以以某种方式解析日期。我仍然不知道如果你创建一个
EventRequest
的类它为什么不起作用就像我上面的原始代码一样,我还看到了 example其中命令对象不是参数。我认为仍然可以解析日期,但我想这是 Grails 的另一个令人困惑的方面。
关于json - 使用自定义转换器时在 Grails 中处理 PUT/POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30940837/