我创建了两个域,作者和书,作者有很多书,书是属于作者的。
class Author {
hasMany = [ books : Book ]
String name
}
class Book {
String title
Author author
}
BookController.groovy 中的这个“保存书”功能
def savebook={
def json = request.JSON
def bookInstance = new Book()
bookInstance.properties = json
bookInstance.author_id = json.author_id
bookInstance.title = json.title
if (bookInstance.validate()) {
bookInstance.save();
def rep = [ respence: "1" ] // save
render rep as JSON
}
else {
def rep = [ respence: "0" ] // not save
render rep as JSON
}
}
我还没有找到该行的解决方案:bookInstance.author_id = json.author_id
在“savebook”中创建作者的书。
我的第二个问题是 JSON 对象的结构应该如何进行 REST POST ?