我有问题!需要获取已经设置了尚未保存的id的json对象。但是在尝试创建对象时,id为null。并且只有在调用save方法生成不同的id时才会设置它。 例如:
objetoJson = {id: "123413215123", name: "Leonan Teixeira", address: {id: "12345", city: "Victory", state: "Bahia"}}
def person = new Person (objetoJson)
对象的结束状态:
Person.id = null Person.Address = null
但我需要存储id。 如果我这样做也会发生 person.properties = objetoJson
我的课程用id' uuid'
进行映射
String id;
static mapping = {
id generator: 'uuid'
}
解决方案
为约束
添加一个bindable: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/
答案 0 :(得分:0)
试试这个:
def jsonSlurper = new JsonSlurper()
def dataPerson = jsonSlurper.parseText('{ "id":"123456789", "nome": "Leonardo Meurer", "age":"29" }')
def person = new Person (dataPerson)