RESTful初探之四(Restlets)

2023-05-16

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

[color=red]The Restlet framework[/color]

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命令操作。

[color=red]The race application[/color]
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:
[/size]
Router router = new Router(this.getContext());
router.attach("order/{order_id}", Order.class);

[size=large]创建一个继承自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逻辑映射起来。[/size]

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;
}
}

[size=large]继承自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上。

[color=red]Race resources[/color]
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.


[color=red]上面讲的都是原理性的东西,下面是具体开发实例:[/color]
[/size]
[url]http://www.lifeba.org/arch/restlet_develop_jax-rs_service_1.html[/url]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RESTful初探之四(Restlets) 的相关文章

随机推荐

  • shell 脚本中的引用问题

    原始代码如下 bin sh myvar 61 34 Hello world 34 echo myvar echo 34 myvar 34 echo 39 myvar 39 echo myvar echo Enter some test re
  • Linux内核的TCP源码入门(一)

    文章目录 前言一 TCP报文段结构1 报文段整体结构2 TCP首部 固定部分3 TCP首部 选项 options 二 TCP接收和发送数据1 TCP的 34 接口 34 2 发送数据3 接收数据3 1 ip层向上调用INET Socket层
  • 【API接口工具】postman-Windows版、Linux安装

    Windows安装 Postman 适用于 Windows 7 及更高版本 下载最新的 Postman 版本 选择并运行该 exe文件以安装 Postman Postman v9 4 是 Postman 的最后一个版本 xff0c 同时支持
  • 四轴飞控DIY调试起飞简明步骤

    四轴飞控DIY调试起飞简明步骤 调试起飞简明步骤Step1 xff1a 飞控配置Step2 xff1a 试飞目标测试内容坐标系 Step3 xff1a 试飞方法1 升降 xff08 Throttle xff09 2 偏航 xff08 yaw
  • PX4模块设计之二十七:LandDetector模块

    PX4模块设计之二十七 xff1a LandDetector模块 1 LandDetector模块简介2 模块入口函数2 1 主入口land detector main2 2 自定义子命令custom command 3 LandDetec
  • 穿越机用途和机架尺寸

    穿越机用途和机架尺寸 1 穿越机的用途2 穿越机的机架3 机架的类型3 1 正X型机架3 2 宽X型机架3 3 长X型机架3 4 Hybrid机架3 5 涵道机架 4 总结 1 穿越机的用途 穿越机按功能分 xff0c 主要分为竞速Race
  • 关于穿越机FPV视频果冻效应的讨论

    关于穿越机FPV视频果冻效应的讨论 1 名词定义2 摄像原理2 1 快门分类2 2 常见传感器2 3 卷帘拍摄 3 产生原因4 解决方法4 1 振动出处4 2 软件方法 辅助作用 4 3 硬件方法 直接办法 5 F450试验机FPV视频问题
  • 四轴飞控DIY Mark4 - 减震

    四轴飞控DIY Mark4 减震 1 DIY Mark42 改进事项2 1 Mark4 5 inches机架2 2 2205 2450KV 无刷电机2 3 电机与机架的TPU防震2 4 飞控防震垫圈2 5 三叶平衡桨 3 试飞效果3 1 视
  • Java的压力测试工具之Jmeter

    size 61 large Apache JMeter是Apache组织开发的基于Java的压力测试工具 用于对软件做压力测试 xff0c 它最初被设计用于Web应用测试但后来扩展到其他测试领域 它可以用于测试静态和动态资源例如静态文件 J
  • 四轴飞控DIY Mark4 - 整理&参数优化

    四轴飞控DIY Mark4 整理 amp 参数优化 1 历程2 参数优化2 1 固件BF4 3 12 2 动态怠速值2 3 滤波参数2 4 电调PWM频率2 5 GPS高度配置2 6 返航速度和高度2 7 线性推力修正2 8 图传频道调整
  • ArduPilot开源飞控系统之简单介绍

    ArduPilot开源飞控系统之简单介绍 1 源由2 了解 amp 阅读2 1 ArduPilot历史2 2 关于GPLv32 3 ArduPilot系统组成2 4 ArduPilot代码结构 3 后续3 1 DIY F4503 2 软件设
  • ArduPilot Kakute F7 AIO DIYF450 之GPS配置

    ArduPilot Kakute F7 AIO DIYF450 之GPS配置 1 源由2 步骤2 1 模块预测试2 2 物理连接2 3 UART配置2 4 Compass使能2 5 GPS使能2 6 校准Compass 3 GPS amp
  • ArduPilot之开源代码框架

    ArduPilot之开源代码框架 1 系统框架2 工程框架2 1 工程目录2 2 代码组成2 3 运行流程 4 硬件传感器总线4 1 I2C4 2 SPI4 3 UART4 4 CAN 5 软件设计概念6 总结7 参考资料 在研读ArduP
  • COPY 一种接近最优的导航网格生成算法以及基于导航网格的寻路算法

    提出背景 xff1a 长距离寻路会出现掉帧现象 xff0c 为了提高寻路速度 xff0c 并为3D环境中的寻路方案提供基础算法实现 目前状况 xff1a 由于3D游戏对帧率要求很高 xff0c 而在游戏中进行一次长距离的寻路可能要花费8 1
  • 解析串口-接收完整数据帧

    在linux下编写串口通讯程序 xff0c 采用select监听串口的可读事件 xff0c 一旦可读 xff0c 调用read 但是我们会发现 xff0c read一次得到的数据通常不是完整的一个数据帧 比如完整数据帧为 但是实际上需要re
  • STL 基本容器 优缺点比较

    总结在先 xff1a xff11 如果需要高效的随机存取 xff0c 不在乎插入和删除的效率 xff0c 使用vector xff1b 2 如果需要大量的插入和删除元素 xff0c 不关心随机存取的效率 xff0c 使用list xff1b
  • STL源码剖析--vector容器

    写在前面 vector是我们在STL中最常用的容器 xff0c 我们对它的各种操作也都了然于胸 然而我们在使用vector的时候总会有一种很虚的感觉 xff0c 因为我们不清楚接口内部是如何实现的 在我们眼里宛如一个黑箱 xff0c 既危险
  • TCP/UDP调试工具的使用

    TCP UDP调试工具下载链接 前文 当我们写好一个TCP UDP的程序时 但是无法通信时 光看代码又找不出原因时 我们可以借助调试工具来检查是服务端还是客户端出现了问题 这样就很大的减少了错误的排查范围 再次感叹一下 这个工具真的很好用
  • 关于利用结构体和联合体数据收发的两种方法

    关于利用结构体和联合体数据收发的两种方法 关于最近接手的小项目 xff0c 有了一些经验 xff0c 所以进行一下记录 文章目录 关于利用结构体和联合体数据收发的两种方法前言一 联合体法二 结构体法小tips 前言 在我们利用自己的板子进行
  • RESTful初探之四(Restlets)

    size 61 large Restlets Restlet项目为 建立REST概念与Java类之间的映射 提供了一个轻量级而全面的框架 它可用于实现任何种类的REST式系统 xff0c 而不仅仅是REST式Web服务 color 61 r