我正在探索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/