【发布时间】:2015-08-19 21:26:43
【问题描述】:
我在 Mac 上使用 Grails 2.3.8。
我希望使用 Grails 快速开发一个 RESTful API。我从 Web 服务的基本文档开始,这里是我的版本: http://grails.org/doc/2.3.8/guide/webServices.html#domainResources
这似乎表明我可以在 Grails 中创建一个简单的域类,然后使用 grails.rest 中的 @Resource 进行注释,如下所示(直接来自上面的文档):
import grails.rest.*
@Resource(uri='/books', formats=['json', 'xml'])
class Book {
String title
static constraints = {
title blank:false
}
}
当我将此文件放入 grails-app/domain 目录并运行应用程序时,文档建议我应该能够使用 cURL 通过 HTTP 添加“Book”对象:
curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Along Came A Spider"}' localhost:8080/myapp/books
但这给了我 HTTP 422,并带有以下消息:
HTTP/1.1 422 Unprocessable Entity
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 05 Aug 2014 01:46:05 GMT
{"errors":[{"object":"Book","field":"title","rejected-value":null,"message":"Property [title] of class [class Book] cannot be null"}]}
谁能告诉我这里缺少什么?我希望这很简单。
【问题讨论】:
-
2.3.8 中的缺陷:https://jira.grails.org/browse/GRAILS-11399,要么升级到 2.3.9,要么降级到 2.3.7
-
确认!升级到最新版本(2.4.3)解决了这个问题。随意添加作为答案。