什么是创建/管理使用Grails的RESTful API的有效途径? [英] What is an efficient way to create/manage RESTful API with grails?
问题描述
我已经建立了我的第一个Grails应用程序。我的URL映射是默认的应用程序提供了什么:
I've built my first grails application. My URL mappings are what the default application provides:
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
塞纳里奥
我有一个名为 ColorController
控制器动作保存
和列表
。它根本是这样的:
I have a controller called ColorController
with actions save
and list
. It simply does something like this:
def save () {
def colorInstance = new Color(params)
colorInstance.save(flush: true)
}
def list () {
[colorList: Color.list, colorTotal: Color.count()]
}
我想建立对这些行动的简单API。
I would like to build a simple API for these actions.
- 保存操作应该接受参数JSON,并提供一个成功的消息,如果记录保存。
- 列表操作应提供名单JSON
问题
- 我应该为API一个独立的URL映射? (例如
的http://&LT结构域GT; /<应用> / REST /控制器/动作
) - 我应该让我的API 一个单独的控制器
- 我使用的春天的安全插件进行身份验证。但在某些时候我可能要验证宁静的API为好。哪些是一些解决方案?
- 如果我用同一个控制器,我怎么能修改这些简单的动作做什么,我需要的。
- Should I make a separate URL mapping for api? (e.g.
http://<domain>/<app>/rest/controller/action
) - Should I be making a separate controller for my API's
- I am using spring security plugin for authentication. But at some point I might want to authenticate the restful api as well. What are some solutions for that?
- If I use the same controller, how can I modify these simple actions to do what I need.
推荐答案
甚至低于找我的意见/回答之前,我建议去逛的太问题为REST风格的WS的Grails的基本理解。
Before even looking below for my opinion/answers I would suggest to visit this SO Question for the basic understanding of RESTful WS in Grails.
评论:
-
的保存操作应该接受参数JSON,并提供一个成功的消息,如果记录保存的 - 保存映射到POST REST风格。取而代之的是JSON体结合
PARAMS
它绑定到要求
。为了访问你只需要在操作方法使用request.JSON
JSON对象。
"The save action should accept parameters as JSON and provide a successful message if the records save" - Save is mapped to POST RESTful. Instead of binding a JSON body to
params
it is bound to therequest
. In order to access the JSON object you just need to userequest.JSON
in the action method.
request.JSON的instanceof的JSONObject
//控制列表()
import grails.converter.JSON
def list () {
[colorList: Color.list, colorTotal: Color.count()] as JSON
}
答案的问题: -
- 的我应该为API?一个单独的URL映射的
通过REST的基本知识外,还须在客户端只能访问(在这种情况下,颜色
)的资源,不应该操心底层的控制器
或操作
。服务器端逻辑应该从客户端抽象。 URL映射是客户端将用来作为申请表。我会在我的URL映射像这样的颜色
资源。
- Should I make a separate URL mapping for api?
Abiding by the basics of REST, the client should only access the resource (
Color
in this case) and should not bother about the underlyingcontroller
oraction
. The server side logic should be abstracted from the client. URL Mapping is what the client would use to as form of request. I would have something like this in my url mapping forColor
Resource.
/彩色/ $ ID(资源:色)?
或
/color/$id?(controller: 'color'){
action = [GET: "list", POST: "save"]
}
-
的我应该做单独的控制器我的API 的 - 取决于在App的设计方式。您也可以在上述控制器作为API。例如,目前我正在使用哪个
AngularJS
在连接到Grails的APP REST风格的前端一个Grails应用程序。为了实现我有一个RestClientController
这可以作为一个API,棱角分明。在相同的应用程序有一个REST API背后的理由是,在未来我们可以公开基础服务比应用程序本身的角
客户端present其他外部客户端Should I be making a separate controller for my API's? - Depends on the way the App is designed. You also can have the above controller as the API. For example, currently I am working on a grails app which used
AngularJS
in the front End which connects to the Grails APP RESTFully. In order to achieve I had aRestClientController
which works as an API to Angular. The rationale behind having a REST api in the same app is that in future we can expose the underlying service to external clients other than theAngular
client present in the app itself.的我使用的春天的安全插件进行身份验证。但在某些时候我可能要验证宁静的API为好。哪些是一些解决方案的 - 你可以在这里使用Spring Security为好。就我而言,我使用的插件,我通过使用插件的注释部分固定
控制器
@Secured
。我有自定义的的OAuth
启用,以及为其交互公司范围的LDAP与AD组授权。I am using spring security plugin for authentication. But at some point I might want to authenticate the restful api as well. What are some solutions for that? - You can use Spring Security here as well. In my case I am using the plugin and I secure the
controller
by using the plugin's annotated component@Secured
. I have customOAuth
enabled as well for authorization which interacts to the company wide LDAP and AD Groups.的如果我使用相同的控制器,我怎么能修改这些简单的动作做什么,我需要 - 我想你应该已经得到了这个问题的答案(后。通过SO问题,我上面提到的)。这里是我的意见,
控制器
动作
可路由到适当的服务
其中不基于所述请求参数的业务实现类。If I use the same controller, how can I modify these simple actions to do what I need. - I think you would have got the answer to this question by now (after going through the SO question I mentioned above). Here is my opinion,
controller
actions
can route to appropriateservice
classes which does the business implementations based on the request parameters.例如,
//Action def show(){ if(params.id){ colorService.getColor() } else { colorService.searchColor() } }
在上面的例子中,URL映射将
/彩色/ 123
或/颜色
。在前一种情况下,它会获得颜色
,并在以后它会搜索的颜色In the above example, the url mapping would be
/color/123
or/color
. In the former case, it will get thecolor
and in the later it will search the colors这篇关于什么是创建/管理使用Grails的RESTful API的有效途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
?