Grails REST POST因元素具有替换标识符列而失败

Grails REST POST因元素具有替换标识符列而失败,rest,grails,grails-3.3,Rest,Grails,Grails 3.3,这是我正在从事的一个真实项目的简化示例。 我有以下领域 class PhoneNumber { String cn PhoneType phoneType String phoneNumber static constraints = { } } class PhoneType { String phoneType String cn static constraints = { } static mappi

这是我正在从事的一个真实项目的简化示例。 我有以下领域

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生成默认的REST控制器生成所有PhoneType PhoneNumber

我正在使用rest配置文件项目。我可以很好地创建一个电话类型

 curl 'http://localhost:8080/phoneType'  -H 'Content-Type: application/json' -X POST -d '{"phoneType":"gg"}'
 {"cn":"2a10618564d228300164d234a4980003","phoneType":"gg"}
问题是我无法使用REST创建具有此电话类型的电话号码。如果我没有指定备用ID(使用默认的“ID”列),我可以

虽然这是可行的

 curl 'http://localhost:8080/phoneNumber'  -H 'Content-Type: application/json' -X POST -d '{"cn":"1","phoneNumber":"9995551212", "phoneType":{"id":"2a10618564d228300164d234a4980003"}}'
我想知道这是否应该起作用并且是框架中的一个bug,或者是否需要一些额外的配置来支持在grails中使用没有“id”作为标识符的REST资源


提前感谢您的指点

您可以尝试将id设置为字符串或UUID(或任何您需要的内容)


我认为您的问题与id无关。默认情况下,grails的属性是强制性的
(nullable:false)
。在JSON中,您没有给出
phoneType
域的
phoneType
属性,您会得到验证错误。您应该在JSON中添加phoneType属性。尝试发送以下JSON:

{
“cn”:“1”,
“电话号码”:“999551212”,
“电话类型”:{
“cn”:“2a10618564d228300164d234a4980003”,
“电话类型”:“gg”
}
}

或者,如果域
phoneType
的属性
phoneType
不是必需的,您可以在域中定义它,如下所示:

static constraints = {
   phoneType nullable: true
}

谢谢你的意见。如果我为电话类型传递“phoneType”:{“id”:“2a10618564d228300164d234a4980003”},它创建的电话类型很好,没有提供电话类型,因为该实体已经存在,并且它将其连接到现有的电话类型域实例。
class PhoneType {

  String phoneType
  String cn = UUID.randomUUID().toString()
  static constraints = {
  }

  static mapping = {
      id name: 'cn'
      id generator: 'uuid'
  }
}
static constraints = {
   phoneType nullable: true
}