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

查看:34
本文介绍了使用 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')
}

场景

我有一个名为 ColorController 的控制器,带有 savelist 操作.它只是做这样的事情:

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:////rest/controller/action)
  • 我是否应该为我的 API 制作一个单独的控制器
  • 我正在使用 spring 安全插件进行身份验证.但在某些时候,我可能也想验证 restful api.对此有哪些解决方案?
  • 如果我使用相同的控制器,我该如何修改这些简单的操作来完成我需要的操作.

推荐答案

在查看我的意见/答案之前,我建议您访问此 SO 问题,用于基本了解 Grails 中的 RESTful WS.

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 RESTful.它不是将 JSON 主体绑定到 params,而是绑定到 request.为了访问 JSON 对象,您只需要在操作方法中使用 request.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 JSONObject 的实例

//控制器列表()

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

问题答案:-

  1. 我应该为 api 做一个单独的 URL 映射吗?遵守 REST 的基本原则,客户端应该只访问资源(在这种情况下为 Color),而不应该打扰底层的 controlleraction.服务器端逻辑应该从客户端抽象出来.URL 映射是客户端将用作请求的形式.我在 Color 资源的 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.

/color/$id?(resource: "color")

/color/$id?(controller: 'color'){
    action = [GET: "list", POST: "save"]
}

  • 我应该为我的 API 制作一个单独的控制器吗? - 取决于应用程序的设计方式.您也可以将上述控制器作为 API.例如,目前我正在开发一个 grails 应用程序,它在前端使用 AngularJS 连接到 Grails 应用程序 RESTFully.为了实现这一目标,我有一个 RestClientController 可以作为 Angular 的 API.在同一个应用程序中拥有 REST api 背后的基本原理是,将来我们可以将底层服务公开给外部客户端,而不是应用程序本身中存在的 Angular 客户端.

    • 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.

      我正在使用 spring 安全插件进行身份验证.但在某些时候,我可能也想验证 restful api.对此有哪些解决方案? - 您也可以在这里使用 Spring Security.就我而言,我正在使用该插件,并通过使用插件的带注释组件 @Secured 来保护 controller.我还启用了自定义 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 问题).这是我的观点,controller actions 可以路由到适当的 service 类,该类根据请求参数进行业务实现.

      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 映射是 /color/123/color.在前一种情况下,它将获取color,而在后一种情况下,它将搜索颜色

      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天全站免登陆