`
xinyangwjb
  • 浏览: 81566 次
  • 性别: Icon_minigender_1
  • 来自: 信阳
社区版块
存档分类
最新评论

RESTful初探之四(Restlets)

 
阅读更多
Restlets
Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务。

The Restlet framework

Restlet application are akin to servlet applications in that they reside in a container,but in practice they are quite different in two major ways,First,Restlets use no direct notion of HTTP or its stateful manifestations such as cookies or sessions ,per se.Second,the Restlet framework is extremely lightweight.As you'll see ,a fully functional RESTful application can be built with a handful of classes that extend from a few core Restlet base classes.Configuration and deployment leverage existing container models,so you simply update the customary web.xml file and deploy a standard Web archive(WAR)file.
Restlet应用类似于servlet应用,他们都处于一个容器中。但是在实际操作中,他们在俩大方面有很大的不同:
一、Restlets没有直接的HTTP概念,也没有他自身的状态表现如cookies和sessions。
二、Restlets框架非常轻量。如你所见,一个全功能的RESTful应用只需少数几个继承自Restlet基础类的类。配置部署现有集成模式,只需简单更新通常的web.xml文件和部署一个标桩WAR文件。

For the most part,the bulk of a RESTful application built with the Restlet framework requires the use of two base classes:Application and Resource.Logically speaking,an Application instance maps URIs to Resource instances.Resource instances do the work of handling the basic CRUD commands,which are,of course ,mapped to GET,POST,PUT,and DELETE.
大部分情况下,基于Restlet框架的RESTful应用需要俩继承类:Application和Resource。从逻辑上讲,一个应用实例映射URIs到Resource实例。Resource实例支持CRUD命令操作。

The race application
You create a starting point with the Restlet framework by extending from the framework's Application class.In this class,you define Resources that respond to URIs.This definition process is done with the framework's Router class.For example,if you have a URI such as order/order_id,you need to specify which object can handle these requests.This object is an instance of the framework's Resource type.You link objects with URIs by attaching them to a Router instance,as in Listing5:

Router router = new Router(this.getContext());
router.attach("order/{order_id}", Order.class);

创建一个继承自Application的出发点。这个类中定义响应URIs的Resources,即框架的Router类实现。例如:URI order/order_id,你需要指定哪个对象来处理这个请求。这个对象是框架Resource类型的实例。对象与URIs的关联通过附加到Router实例下来实现。

So in this example, the URI order/order_id is logically mapped to an Order class (which, in turn, extends Resource).
这个例子将Order类与URI order/order_id逻辑映射起来。


Acme Racing has the four logical RESTful URIs that you've already defined - four patterns that work with various aspects of races and runners:
      * /race
      * /race/race_id
      * /race/race_id/runner
      * /race/race_id/runner/runner_id
你为Acme Racing 已经定义了四个逻辑RESTful URIs ——四种模式为各方面races和runners服务。

The behavior of each URI(such as if it works with POST,DELETE,GET,and so on)is'not important at this point.The behavior of each Resource is the job of a Resource instance; however,the Application instance is used to map these URIs to (yet-to-be-defined)Resources via a Router instance,as shown in Listing6:
public class ReceApplication extends Application{
      public RaceApplication(Context context){
       super(context);
    }
      public Restlet createRoot(){
       Router router = new Router(this.getContext());
       router.attach("/race",RacesResource.class);    
       router.attach("/race/{race_id}",RaceResource.class);
       router.attach("/race/{race_id}/runner",RaceRunnersResource.class);
       router.attach("/race/{race_id}/runner/{runner_id}",
       RaceRunnerResource.class);
       return router;
    }
}

继承自Application的实例被用来映射URIs到Resources,通过一个Router实例,如上面的代码所示。

    The base class,Application,is an abstract class.Extending classes must implement the createRoot() method.In this method, you can create a Router instance and attach Resources to URIs as i've done in List6.
Application是一个抽象类,他的子类必须实现createRoot方法,这个方法中,我们可以创建Router实例附加Resources到URIs上。

Race resources
Now that you've defined the Application instance to handle four different URI patterns,you must implement the four Resources.
Resource types in the Restlet framework are known as Restlets.They are the heart of any RESTful application developed with the Restlet framework.Unlike the Application type.the base Resource class is not abstract.It's more like a template with default behavior that you can override as needed.
现在你已经定义了四种URI模式在Application实例中。现在你必须再实现4个Resources。
Resource类型在Restlet框架中被称为Restlets。他们是Restlet框架下RESTful应用的核心。不同于Application类型,基础Resource类不是抽象的,他更想一个有默认方法的模板,你只需重写你需要的方法。

At a high level,Resource has four methods that require overriding.Not coincidentally,they map to the basic HTTP commands that are the touchstone of REST-GET,POST,PUT,DELETE.because the Resource class is nonabstract,the framework requires a paired method to be implemented for desired behavior to be invoked.For instance,if you want a particular resource to respond to DELETE requests,you would first implement the delete() method.Second, you also must implement the allowDelete() method and have this method return true(it defaults to false).By default,the corresponding PUT,POST,and DELETE allow methods return false,and the alllowGet() method return true.this means for read-only Resources,you need to override only one method(instead of two in the other three cases).You can alternatively call the setModifcation(true) in a Resource class and thus not have to override individual HTTP verb allow methods.
Resource类有四个方法需要被重写。这并非巧合。他们映射到HTTP的四种命令,是REST-GET,POST,PUT,DELETE的试金石。因为Resource类不是抽象的,REST框架需要一系列的方法去实现所期待的行为去被调用。例如,如果你想让一个指定的resource去响应一个DELETE请求,首先你要实现delete()方法,然后你必须实现allowDelete()方法并且使这个方法返回值为true。
默认情况下,相应的PUT,POST,and DELETE方法允许返回值为false,alllowGet()方法返回为ture。这意味着对于只读Resources,你只需重载一个方法(其它几种情况要俩方法)。你可以在一个resource类中选择性地访问setModifcation(true)方法,并且因此不必重写个体的HTTP动词方法。

For instance,the RacesResource is intended to respond to GET requests with XML document that describes the races in the system.Users can also create new races via this Resource type.Therefore,the RacesResource class overrides at least three methods from the Resource base class:
       *getRepresentation()
       *allowPost()
       *post()
Remember, Resources instances, by default, are read-only. Hence the allowGet() method doesn't need to be overridden.


上面讲的都是原理性的东西,下面是具体开发实例:

http://www.lifeba.org/arch/restlet_develop_jax-rs_service_1.html


分享到:
评论

相关推荐

    Python Flask高级编程之RESTFul API前后端分离精讲第七章节

    Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API...

    Python Flask高级编程之RESTFul API前后端分离精讲第四章节

    Python Flask高级编程之RESTFul API前后端分离精讲第四章节Python Flask高级编程之RESTFul API前后端分离精讲第四章节Python Flask高级编程之RESTFul API前后端分离精讲第四章节Python Flask高级编程之RESTFul API...

    Python Flask高级编程之RESTFul API前后端分离精讲第一讲解

    Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask...

    C# 一个简单的 Restful 服务端和 Restful 客户端 Demo

    本示例是关于如何使用C#语言创建一个简单的RESTful服务端以及对应的RESTful客户端。以下是相关知识点的详细说明: 1. **RESTful原则**:REST(Representational State Transfer)的核心思想是资源(Resource)和...

    Python Flask高级编程之RESTFul API前后端分离精讲第二章节

    Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API...

    Restful C# 服务端篇之实现RestFul Service开发(简单实用)

    在IT行业中,RESTful(Representational State Transfer)是一种软件架构风格,用于设计网络应用程序,尤其在Web服务领域广泛应用。C#作为.NET框架的主要编程语言,提供了丰富的工具和技术来实现RESTful服务。本篇...

    Java Restful Web 源代码,Java Restful Web 源代码

    Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web...

    谷歌浏览器restful请求插件

    总结来说,谷歌浏览器RESTful请求插件是开发人员必备的工具之一,尤其对于需要频繁与Elasticsearch交互的项目。通过这个插件,用户可以方便地发起REST请求,测试和调试API,同时学习和理解RESTful架构风格。无论是在...

    C#服务端RestFul Service-经验案例.doc

    使用C#语言可以实现一个Rest风格的Web服务供外部调用,主要包括以下四点:定义service的契约、定义URL Routing、实现service、为服务编写宿主程序。例如,定义服务契约可以使用GET和POST方式访问,使用VS2015创建一...

    c# 服务端调用RestFul Service的方法

    RESTful API 使用 HTTP 协议定义了四种主要的操作:GET、POST、PUT 和 DELETE,分别对应于资源的获取、创建、更新和删除。 #### 构建 RESTful 风格的 API 构建 RESTful 风格的 API 时,通常会遵循以下几点建议: ...

    restful接口文档模板

    ### RESTful接口文档模板知识点解析 #### 一、RESTful接口概述 REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,可以使用XML或者JSON格式传输数据,一般用于...

    浅谈java调用Restful API接口的方式

    "Java调用Restful API接口的方式" Java调用Restful API接口是Java开发中非常重要的一部分,了解Java调用Restful API接口的方式可以帮助开发者更好地理解和使用相关技术。本文将详细介绍Java调用Restful API接口的...

    restful接口示例代码

    RESTful接口是一种遵循REST(Representational State Transfer,表述性状态转移)架构约束的Web服务设计风格,用于构建可扩展、高性能的互联网应用程序。REST强调通过HTTP协议中的动词(GET、POST、PUT、DELETE等)...

    restful 接口开发规范(RESTfulAPIdesignguide)

    在开发RESTful接口时,我们需要遵循一定的设计规范来确保接口的一致性、可维护性和易用性。RESTful API(Representational State Transfer,也称为RESTful web服务)是一种提供互联网计算机系统间互操作性的方法。...

    httpclient和RestfuL风格上传下载文件

    在Java开发中,HTTPClient和RESTful风格的接口被广泛用于实现文件的上传与下载功能。HTTPClient是一个强大的HTTP客户端库,而RESTful是一种轻量级的、基于HTTP协议的软件架构风格,常用于构建Web服务。在分布式系统...

    Restful风格编程面试题

    Restful风格的请求使用“url+请求方式”表达一次请求目的,HTTP协议里有四个表达操作方式的动词: * GET:用于获取资源 * POST:用于新建资源 * PUT:用于更新资源 * DELETE:用于删除资源 例如: * /user/1 GET ...

    springmvc之restful风格CRUD

    本文将深入探讨如何在Spring MVC中实现RESTful风格的CRUD操作,这对初学者尤其有价值。 首先,了解REST(Representational State Transfer,表述性状态转移)是一种网络应用程序的设计风格和开发方式,基于HTTP协议...

    RESTful Java Web Services

    ### RESTful Java Web Services #### 一、RESTful Web服务概览 REST(Representational State Transfer)是一种软件架构风格,最初由Roy Fielding在他的博士论文中提出。它定义了一种简单且灵活的方法来创建分布式...

    restFul.Net

    在RESTful.NET中,`restService`是核心概念之一,它代表了一个服务端的资源处理单元。通过定义RESTful接口,开发者可以构建出易于理解、可维护的API。RESTful服务通常由一组URI(统一资源标识符)组成,每个URI对应...

Global site tag (gtag.js) - Google Analytics