【发布时间】:2015-06-27 15:23:36
【问题描述】:
我在 grails 中使用 restful 服务。我有 2 节课 Person
import grails.rest.Resource
@Resource(formats=['json', 'xml'])
class Person {
String name
Address address
static constraints = {
}
}
和Address
import grails.rest.Resource
@Resource(formats=['json', 'xml'])
class Address {
String house
static constraints = {
}
}
在引导程序中,我可以按如下方式创建新的人员条目
new Person(name:"John" , address: new Address(house:"John Villa").save()).save();
但问题是我想使用带有 JSON 数据的 POST 请求来做同样的事情。 我将 UrlMappings.Groovy 设置为
"/app/person"(resources: "person", includes: ['index', 'show', 'save', 'update', 'delete', 'create', 'patch'])
"/app/address"(resources: "address", includes: ['index', 'show', 'save', 'update', 'delete', 'create', 'patch'])
然后我尝试通过使用 JSON 数据向“/app/person”发送 POST 请求来使用 POSTMAN REST 客户端
{
"name":"john" ,
"address": "{'house' :'sample address' }"
}
但它给出了错误 422 无法处理的实体。
如何使用 JSON 做同样的事情?我想使用 POST 和 PUT 方法进行插入和更新。
【问题讨论】:
-
你能告诉我你的 /app/person/index 控制器的来源吗?
-
我没有一个名为 PersonController 的控制器。当我使用
"/app/person"(resources: "person")这个 url 映射时,对 Person 对象的 crud 操作由 grails 自动设置。
标签: rest grails grails-orm grails-domain-class grails-2.4