如何通过REST post调用存储应用程序的数据

时间:2012-01-31 13:49:56

标签: rest grails

您正在开发我的第一个grails RESTful应用程序...在我已经映射了post请求保存method.its显示一些错误,如内部服务器错误..可以任何人请帮我简要介绍如何使用该帖子通过REST Post请求保存数据的方法......? 我的保存方法如下..

def save = {
    def xml = request.XML
    def post = new ImageProperties()
    post.content = xml.content.text()
    def markup
    if (post.save()) { markup = { status("OK") } }
    else { markup = { status("FAIL") } }
    render contentType: "text/xml; charset=utf-8", markup } }

和ImageProperties类如下......

class ImageProperties {
    static hasMany={categories:Categories}
    String name
    String place
    String description
    String path
    String category
    Categories categories
}

2 个答案:

答案 0 :(得分:0)

您要将发布的数据分配到content的预期ImageProperties属性

def post = new ImageProperties()
post.content = xml.content.text()

但你的实体中的那个属性是什么?

class ImageProperties {
    static hasMany={categories:Categories}
    String name
    String place
    String description
    String path
    String category
    Categories categories
}

更新:如果您只想填充所有属性,可以使用域类构造函数:

def post = new ImageProperties(xml.content)

请参阅Grails手册的RESTData Binding部分中的详细信息。

答案 1 :(得分:0)

嗨,经过长时间的试错法,我找到了答案。希望这会对很多用户有所帮助......

def save = {
    def xml = request.XML
    def post = new ImageProperties()
    post.name = xml.name.text()
    post.place = xml.place.text()
    post.path = xml.path.text()
    post.description = xml.description.text()
    post.category = xml.category.text()
    post.categories = Categories.get(xml.categories.@id.text())
    def markup

    if (post.save()) {
        markup = {
            status("OK")
        }
     } else {
            markup = {
            status("FAIL")
        }
    }
    render contentType: "text/xml; charset=utf-8",
    markup
}