实现一个Grails应用一个REST API [英] Implement a REST API in a Grails app

查看:1102
本文介绍了实现一个Grails应用一个REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是使一个Grails应用程序提供最佳方法的的RESTful API 的(一些CRUD操作为主),可以通过Web服务,如使用当你想建立一个相应的的iOS 的应用程序到你的基于浏览器的应用程序或其他任何东西。

I was wondering what would be the best approach to make a Grails app offer a RESTful API (some CRUD actions mainly) that can be used by a web service, e.g. when you want to build a corresponding iOS app to your browser-based app or anything else.

我想在我的Grails应用程序,需要从 www.mywebapp.com/api/someAction 电话,这样我可以重用的服务层建立一个独立的部分组成。我会怎么做URL映射呢?只有一大 ApiController 听起来并不很时髦。

I thought of building a separate part in my Grails application that takes calls from www.mywebapp.com/api/someAction so that I can reuse the Service layer. How would I do the URL mapping then? Only having one big ApiController does not sound very groovy.

或者是还有什么更好的方法,我不知道的?这种方法的必须支持类似的的OAuth 的验证是谁打来的Web服务的用户。

Or is there any better approach I did not know of? This approach must support something like OAuth to authenticate the user who is calling the Web service.

推荐答案

Grails能够的绝对的提供REST API,但是困难的在这方面的水平取决于如何的 成熟(又名如何基于REST)你想要的API来定。

Grails can definitely provide a REST api, but the level of difficulty in doing so varies depending on how mature (aka. how RESTful) you want the API to be.

获取RESTfullness,你在哪里使用HTTP动词的满量程操纵资源的JSON或XML重新presentations和利用HTTP响应codeS的基本水平,为pretty容易。主要有3个,以获得该到位:

Getting a basic level of RESTfullness, where you are manipulating json or xml representations of resources using the full span of HTTP verbs and leveraging the HTTP response codes, is pretty easy. There are 3 main pieces to getting that in place:


  1. URL映射

下面就是我如何写上最近的一个项目我的URL映射的例子,让更多的REST风格的URL:

Here's an example of how I wrote my URL mappings on a recent project to allow for more RESTful URLs:

// RESTful list mapping
name restEntityList: "/$controller"(parseRequest: true) {
    action = [GET: "list", POST: "save"]
}

// RESTful entity mapping
name restEntity: "/$controller/$id"(parseRequest: true) {
    action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"]
    constraints {
        id matches: /\d+/
    }
}


  • 内容协商

    这Grails能够处理内容协商的3种不同的方式使框架非常灵活,让您以支持更广泛的客户谁可能无法把事情像接受HTTP标头。

    The 3 different ways that Grails can handle content negotiation make the framework very flexible, allowing you to support a much broader range of clients who may not be able to set things like the Accept HTTP header.

    您可以使用内容协商使用以不同的方式不同的请求作出回应在withFormat 基于什么客户已经表示,他们希望块。这种强大的能力,也可以用来版本的API,就像 Github上没有如何的。

    You can use the content negotiation to respond to different requests in different ways using the withFormat block based on what the client has indicated they want. This powerful ability can also be used to version your API, much like how Github does.

    响应状态。

    HTTP已经把它建成一个伟大的响应机制,可以让你在架构利用天生的能力,如缓存能力和indemnipotent操作。虽然有些Web浏览器不处理某些响应codeS很优雅,使用API​​可以使用它们的客户端应用,大大简化了他们的内部code。

    HTTP already has a great response mechanism built into it that allows you to leverage innate abilities in the architecture, like cacheability and indemnipotent operations. While some web browsers don't handle certain response codes very gracefully, client applications using your API can use them to greatly simplify their internal code.

    一,使您的应用程序的RESTful并保持干燥,同时最好的方法是利用控制器脚手架尽可能,因为CRUD本质上是对所有的域对象相同。 上使默认的控制器更本文 REST风格和 上简化了默认控制器是从脚手架获得更多的权力都是伟大的资源。

    DRY REST

    One of the best ways to make your application RESTful and keep it DRY at the same time is to leverage the controller scaffolding as much as possible, since CRUD is essentially the same for all domain objects. This article on making the default controller more RESTful, and this article on simplifying the default controller are both great resources for getting more power from the scaffolding.

    一旦你到这一点,你必须为你的Grails应用一个pretty功能REST API。你可以做所有基本的CRUD操作和资源是相当容易的工作。

    Once you get to that point, you have a pretty functional REST API for your grails application. You can do all basic CRUD operations and the resources are fairly easy to work with.

    梯形的下一个水平一个真正的RESTful API的超媒体,但是,是的的更难达到。修复这是路线图的Grails,但目前它是相当痛苦的。这些作品分别是:

    The next levels of the ladder to a true RESTful hypermedia API, however, are much harder to attain. Fixing this is on the road map for Grails, but currently it's rather painful. These pieces are:


    1. 超媒体资源

    2. 内容类型

    3. 版本

    值得庆幸的是,有插件,使定义自定义marshallers非常容易的,这让我们很容易覆盖REST难题这三个残片。

    Thankfully, there is a plugin that makes defining custom marshallers very easy, which allows us to fairly easily cover those three remaining pieces of the REST puzzle.

    最后,还有一个固定整个事情的方面。一般情况下,春季安全将你抱在非常有利尽可能保证用户的访问您的API。由于大多数API的访问是从应用程序,而不是用户可见,基本或摘要身份验证通常要最简单的方法。有的一个的OAuth插件构建在春季安全。我没有亲自使用过它,所以我不能保证它的稳定性,但它看起来pretty对我好。

    Finally, there is the aspect of securing the whole thing. In general, Spring Security will hold you in good stead as far as securing user-access to your api. Since most API access is from an application, and isn't user-visible, basic or digest authentication is usually the simplest way to go. There is an OAuth plugin that builds on Spring Security. I have not personally used it so I can't vouch for it's stability, but it looks pretty good to me.

    在一般情况下,Grails是灵活和强大到足以做REST非常,非常好,但工作尚未完成尚未让它做REST干净外的开箱。

    In general, Grails is flexible and powerful enough to do REST very, very well, but the work has not been done yet to make it do REST cleanly out-of-the-box.

    这篇关于实现一个Grails应用一个REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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