这是我正在从事的真实项目的简化示例。
我有以下 Realm 。
class PhoneNumber {
String cn
PhoneType phoneType
String phoneNumber
static constraints = {
}
}
class PhoneType {
String phoneType
String cn
static constraints = {
}
static mapping = {
id name: 'cn'
id generator: 'uuid'
}
}
简单,一部电话,具有一部电话类型。
PhoneType使用cn的备用ID列
我使用
grails generate-all PhoneType PhoneNumber
生成默认的REST Controller 我正在使用一个休息文件项目。我可以创建一个PhoneType很好。
curl 'http://localhost:8080/phoneType' -H 'Content-Type: application/json' -X POST -d '{"phoneType":"gg"}'
{"cn":"2a10618564d228300164d234a4980003","phoneType":"gg"}
问题是我无法使用REST使用此PhoneType创建一个PhoneNumber。如果我未指定备用ID(使用默认的“id”列),则可以。
curl 'http://localhost:8080/phoneNumber' -H 'Content-Type: application/json' -X POST -d '{"cn":"1","phoneNumber":"9995551212", "phoneType":{"cn":"2a10618564d228300164d234a4980003"}}'
{"message":"Property [phoneType] of class [class PhoneType] cannot be null","path":"/phoneNumber/index","_links":{"self":{"href":"http://localhost:8080/phoneNumber/index"}}}
虽然这有效
curl 'http://localhost:8080/phoneNumber' -H 'Content-Type: application/json' -X POST -d '{"cn":"1","phoneNumber":"9995551212", "phoneType":{"id":"2a10618564d228300164d234a4980003"}}'
我想知道这是否应该工作并且是框架中的错误,或者是否需要一些其他配置才能在没有“id”作为标识符的grails中使用REST资源。
在此先感谢您提供任何指导。
最佳答案
您可以尝试将ID设置为String或UUID(或任何您需要的值)。
class PhoneType {
String phoneType
String cn = UUID.randomUUID().toString()
static constraints = {
}
static mapping = {
id name: 'cn'
id generator: 'uuid'
}
}