我有问题! 需要获取一个已经设置了尚未保存的 ID 的 JSON 对象。 但是当尝试创建对象 ID 时,为 null。并且仅在调用生成不同 id 的 save 方法时设置它。前任:
objetoJson = {id: "123413215123", name: "Leonan Teixeira", address: {id: "12345", city: "Victory", state: "Bahia"}}
def person = new Person (objetoJson)
对象的结束状态:
Person.id = 空Person.Address = null
但我需要存储 id。如果我这样做,也会发生同样的情况person.properties = objetoJson
我的类映射为 id 'uuid'
String id;
static mapping = {
id generator: 'uuid'
}
溶液
将可绑定对象:True 添加到约束中
class MyDomain {
static constraints = {
// allow binding of "id" attribute (e.g. in constructor or url parameters)
id bindable: true
}
}
http://www.redcube.de/assigning-id-domain-objects-grails-via-constructor/
试试这个:
def jsonSlurper = new JsonSlurper()
def dataPerson = jsonSlurper.parseText('{ "id":"123456789", "nome": "Leonardo Meurer", "age":"29" }')
def person = new Person (dataPerson)