gpt4 book ai didi

rest - 使用Grails中的Rest查找特定ID

转载 作者:行者123 更新时间:2023-12-02 14:40:50 24 4
gpt4 key购买 nike

我正在探索Grails 3.1.4中的RESTful域功能,并且得到了奇怪的结果。例如,我有这个域对象(没有对应的 Controller ):

package resttest

import grails.rest.*

@Resource(readOnly = false, formats = ['json', 'xml'])
class Book {
String name
}

查询所有对象似乎可以正常工作:
$ curl http://localhost:8080/book
[{"id":1,"name":"The Time Machine"},{"id":2,"name":"Moby Dick"}]

但是获取特定对象失败:
$ curl http://localhost:8080/book/1
{"message":"Not Found","error":404}

我一定做错了什么,但简单的我看不到。

最佳答案

需要在@Resource中提供有效的uri,之后可以访问端点。我将为Book资源使用以下内容(注意,使用复数books而不是book)。

import grails.rest.Resource

@Resource(uri = "/books", readOnly = false, formats = ['json', 'xml'])
class Book {
String name
}

结果:
$ curl http://localhost:8080/books
[{"id":1,"name":"The Time Machine"},{"id":2,"name":"Moby Dick"}]
$ curl http://localhost:8080/books/1
{"id":1,"name":"The Time Machine"}

关于rest - 使用Grails中的Rest查找特定ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com