什么是创建/管理使用Grails的RESTful API的有效途径? [英] What is an efficient way to create/manage RESTful API with grails?

查看:131
本文介绍了什么是创建/管理使用Grails的RESTful API的有效途径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了我的第一个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')
}

塞纳里奥

我有一个名为 Col​​orController 控制器动作保存列表。它根本是这样的:

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 the request. In order to access the JSON object you just need to use request.JSON in the action method.

request.JSON的instanceof的JSONObject

//控制列表()

import grails.converter.JSON
def list () {
   [colorList: Color.list, colorTotal: Color.count()] as JSON
}

答案的问题: -


  1. 我应该为API?一个单独的URL映射
    通过REST的基本知识外,还须在客户端只能访问(在这种情况下,颜色)的资源,不应该操心底层的控制器操作。服务器端逻辑应该从客户端抽象。 URL映射是客户端将用来作为申请表。我会在我的URL映射像这样的颜色资源。

  1. 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 underlying controller or action. 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 for Color 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 a RestClientController 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 the Angular 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 custom OAuth 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 appropriate service 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 the color and in the later it will search the colors

      这篇关于什么是创建/管理使用Grails的RESTful API的有效途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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