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

如何通过REST post调用存储应用程序的数据,rest,grails,Rest,Grails,您好,我正在开发我的第一个grails RESTful应用程序。。。因为我已经将post请求映射到save方法。它显示了一些错误,例如,内部服务器错误..任何人都可以帮助我简要介绍如何使用post方法通过REST post请求保存数据。。。? 我的保存方法如下 def save = { def xml = request.XML def post = new ImageProperties() post.content = xml.content.text() d

您好,我正在开发我的第一个grails RESTful应用程序。。。因为我已经将post请求映射到save方法。它显示了一些错误,例如,内部服务器错误..任何人都可以帮助我简要介绍如何使用post方法通过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
}

您正在将发布的数据分配给您的
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手册的和一节中的更多内容。

您好,经过长时间的尝试,我找到了答案。。希望这将有助于许多用户

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
}

请张贴您使用的代码,并更详细地描述错误。不在注释中!请编辑上面的文章,添加代码,并用四个空格缩进:)无需换行。只需按原样发布代码,并在每行缩进四个空格。欢迎使用堆栈溢出!请花一些时间熟悉问题发布界面,以便正确格式化您提供的任何代码示例。正确格式化的内容鼓励其他用户通过让他们更好地理解您的问题来帮助您。此外,请发布grails给出的堆栈跟踪和准确的错误消息。(还有,这是什么版本的grails?)。。我试图在post.content=xml.content.text()的帮助下将发布数据的内容直接存储到ImageProperties类中。。。有可能吗。。?如果不是的话,我们怎么做呢..为此,你可以使用域类构造函数,比如“newImageProperties(xml.content)”-我扩展了anwer。