REST风格的Grails应用程序:干燥UrlMapping [英] RESTful grails application: DRYing up UrlMapping

查看:101
本文介绍了REST风格的Grails应用程序:干燥UrlMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  • 标签

  • 网址

  • 用户



该应用程序有一个经典的Web界面,行政。
我们希望通过RESTful API将应用程序中的资源公开给客户端,并且我们不希望应用程序的这部分混乱我们已有的控制器和代码。
因此,我们想出了以下内容:

如果Web界面提供 host / app_path / url / [list | show | create ] 我们希望REST API在 / host / app_path / rest / url



  class UrlMappings {
static mappings = {
/ $ controller / $ action?/ $ id?{

}

/ *添加新网址并列出它们* /
/ rest / url{
controller =urlRest
action = [POST:save,PUT:save,GET:list,DELETE:error]
}

/ *访问单个网址* /
/ rest / url / $ id{
controller =urlRest
action = [POST:update ,PUT:update,GET:show,DELETE:delete]
}

/ *非垃圾邮件* /
/ rest / url / $ action?/ $ id?{
controller =urlRest
}

/(view:/ index)
500 (图: '/错误')


$ / code>

问题是,这不完全是这里最干燥的事情。随着我们添加更多资源(如标签),情况会变得更糟。他们会翻译成另外三个非常类似的代码块......



非crud函数将会像搜索特定的标准等一样...

p>

我们尝试使用循环生成映射闭包,但没有成功。我们在这里完全走错了路吗?

解决方案

我会推荐以下映射:

 / rest / url / $ id?(resource:urlRest)



<

  GET显示
PUT更新
POST保存
删除删除

我明白你为什么想要映射/ rest / url POST保存和/ rest / url / id PUT更新,但这违背了那些动词的意思。一个PUT应该是唯一的方法来添加一个新的url和POST是更新url的唯一方法。如果您的约束是保持您的当前控制器代码不变,那么按照您的布局方式进行操作将会起作用并且可能是最好的方法。然而,我的猜测是,你的控制器可能已经被编码来处理默认映射就好(更新/删除给出错误,如果没有ID,显示重定向,如果没有ID等)。


Let's say we have a grails web application exposing several resources.

  • tags
  • urls
  • users

The application has a classical web-interface which the users interact with and some administration. We want to expose the resources from the application to clients via a RESTful API and we don't want that part of the app to clutter up the controllers and code we already have. So we came up with the following:

If the web interface offers host/app_path/url/[list|show|create] we want the REST API to be at /host/app_path/rest/url.

So we ended up with the following UrlMappings file:

class UrlMappings {
static mappings = {
    "/$controller/$action?/$id?"{

    }

    /* adding new urls and listing them */
    "/rest/url"{
        controller = "urlRest"
        action = [POST: "save", PUT: "save", GET: "list", DELETE:"error"]
    }

    /* accessing a single url */
    "/rest/url/$id"{
        controller = "urlRest"
        action = [POST: "update", PUT: "update", GET: "show", DELETE: "delete"]
    }

    /* non-crud stuff on urls */
    "/rest/url/$action?/$id?"{
        controller = "urlRest"
    }

    "/"(view:"/index")
    "500"(view:'/error')
}
}

The problem is, that this isn't exactly the most DRY thing here. It gets worse as we add more resources such as tags. They would translate to yet another three blocks of very similar code...

The non-crud functions will be things like searching with specific criterions and such...

We tried generating the mapping closures with a loop, but without success. Are we completely on the wrong track here?

解决方案

I would recommend the following mapping:

"/rest/url/$id?"(resource:"urlRest")

Below is the HTTP method to action mapping that this would create for the urlRestController:

GET         show
PUT         update
POST        save
DELETE      delete

I see why you might want to map /rest/url POST to save and /rest/url/id PUT to update, but that goes against the meaning of those verbs. A PUT should be the only way to add a new url and POST the only way to update a url. Doing it the way you have laid out would work and might be the best way if your constraint is to keep your current controller code untouched. However, my guess is that you controller might already be coded to handle the default mappings just fine (update/delete give error if no id, show redirects to list if no id, etc.).

这篇关于REST风格的Grails应用程序:干燥UrlMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆