【问题标题】:Grails 2.3.8 RESTful APIGrails 2.3.8 RESTful API
【发布时间】: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"}]}

谁能告诉我这里缺少什么?我希望这很简单。

【问题讨论】:

标签: rest grails


【解决方案1】:

正如@raffian 上面在评论中提到的,这是由于 Grails 2.3.8 中的一个错误:

https://jira.grails.org/browse/GRAILS-11399

升级解决了这个问题。

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多