我有个问题!需要获取一个已经设置了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 =空
但我需要存储ID。
如果我做同样的事情
person.properties =对象
我的类(class)被映射为id'uuid'
String id;
static mapping = {
id generator: 'uuid'
}
解决方案
在约束中添加一个bindable:trueclass 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)
关于json - 使用Grails中预定义的ID从JSON创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27358145/