RESTLET框架学习

article/2025/9/21 9:04:07

通过代码学REST——RESTLET框架学习

复制代码
/**
 *  
 * @摘自
 * 
http://www.restlet.org/documentation/1.1/firstResource
 * 一个服务器端的组件,用来启动一个服务,监听8182端口,并将/firstResource的URL请求,交给
 * FirstResourceApplication
 
*/
public   class  FirstResourceServerMain
{
    
public   static   void  main(String[] args)  throws  Exception
    {
        
//  Create a new Component.
        Component component  =   new  Component();
        
//  Add a new HTTP server listening on port 8182.
        component.getServers().add(Protocol.HTTP,  8182 );
        
//  Attach the sample application.
        component.getDefaultHost().attach( " /firstResource " , new  FirstResourceApplication());
        
//  Start the component.
        component.start();
    }
}
复制代码
复制代码
package  firstSteps;

import  java.util.concurrent.ConcurrentHashMap;
import  java.util.concurrent.ConcurrentMap;

import  org.restlet.Application;
import  org.restlet.Restlet;
import  org.restlet.Router;
-----------------------------------------------------这个类类似于Struts中的中央控制器的作用--------------------------------------------------
/* */
public   class  FirstResourceApplication  extends  Application
{  
      
        
/**  The list of items is persisted in memory.  */   
        
private   final  ConcurrentMap < String, Item >  items  =    new  ConcurrentHashMap < String, Item > ();  
      
        
/**  
         * Creates a root Restlet that will receive all incoming calls. 
         * In general, instances of Router, Filter or Handler classes will be used as initial application Restlet. 
         * The default implementation returns null by default. This method is intended to be overriden by subclasses. 
         * 
         
*/   
        @Override  
        
public  Restlet createRoot() 
        {  
            
//  Create a router Restlet that defines routes.  
            Router router  =   new  Router(getContext());  
            
//  Defines a route for the resource "list of items"  
            router.attach( " /items " , ItemsResource. class );  
            
//  Defines a route for the resource "item"  
            router.attach( " /items/{itemName} " , ItemResource. class );  
      
            
return  router;  
        }  
      
        
/**  
         * Returns the list of registered items. 
         *  
         * 
@return  the list of registered items. 
         
*/   
        
public  ConcurrentMap < String, Item >  getItems()
        {  
            
return  items;  
        }  
    }  

 

复制代码
package  firstSteps;

import  java.io.IOException;

import  org.restlet.Client;
import  org.restlet.data.Form;
import  org.restlet.data.Protocol;
import  org.restlet.data.Reference;
import  org.restlet.data.Response;
import  org.restlet.resource.Representation;

public   class  FirstResourceClientMain 
{
        
public   static   void  main(String[] args)  throws  IOException
        {  
                
//  Define our Restlet HTTP client.  
                Client client  =   new  Client(Protocol.HTTP);   // 作为一个通用连接器
                
//  The URI of the resource "list of items".  
                Reference itemsUri  =   new  Reference( " http://localhost:8102/firstResource/items " );  
                
//  Create a new item  
                Item item  =   new  Item( " item1 " " this is an item. " );  
                Reference itemUri 
=  createItem(item, client, itemsUri);  
                
if  (itemUri  !=   null ) {  
                    
//  Prints the representation of the newly created resource.  
                    get(client, itemUri);  
                }  
          
                
//  Prints the list of registered items.  
                get(client, itemsUri);  
          
                
//  Update the item  
                item.setDescription( " This is an other description " );  
                updateItem(item, client, itemUri);  
          
                
//  Prints the list of registered items.  
                get(client, itemsUri);  
          
                
//  delete the item  
                deleteItem(client, itemUri);  
          
                
//  Print the list of registered items.  
                get(client, itemsUri);  
            }  
          
            
/**  
             * Try to create a new item. 
             * 
             * 
@param  item 
             *                the new item. 
             * 
@param  client 
             *                the Restlet HTTP client. 
             * 
@param  itemsUri 
             *                where to POST the data. 
             * 
@return  the Reference of the new resource if the creation succeeds, null 
             *         otherwise. 
             
*/   
            
public   static  Reference createItem(Item item, Client client,   Reference itemsUri)
            {  
                
//  Gathering informations into a Web form.  
                Form form  =   new  Form();  
                form.add(
" name " , item.getName());  
                form.add(
" description " , item.getDescription());  
                Representation rep 
=  form.getWebRepresentation();  
                
//  Launch the request  
                Response response  =  client.post(itemsUri, rep);  
                
if  (response.getStatus().isSuccess())
                {  
                    
return  response.getEntity().getIdentifier();  
                }  
          
                
return   null ;  
            }  
          
            
/**  
             * Prints the resource's representation. 
             * 
             * 
@param  client 
             *                client Restlet. 
             * 
@param  reference 
             *                the resource's URI. 
             * 
@throws  IOException 
             
*/   
            
public   static   void  get(Client client, Reference reference)   throws  IOException 
               {  
                Response response 
=  client.get(reference);  
                
if  (response.getStatus().isSuccess()) {  
                    
if  (response.isEntityAvailable()) {  
                        response.getEntity().write(System.out);  
                    }  
                }  
            }  
          
            
/**  
             * Try to update an item. 
             * 
             * 
@param  item 
             *                the item. 
             * 
@param  client 
             *                the Restlet HTTP client. 
             * 
@param  itemUri 
             *                the resource's URI. 
             
*/   
            
public   static   boolean  updateItem(Item item,   
                                             Client client,   
                                             Reference itemUri) {  
                
//  Gathering informations into a Web form.  
                Form form  =   new  Form();  
                form.add(
" name " , item.getName());  
                form.add(
" description " , item.getDescription());  
                Representation rep 
=  form.getWebRepresentation();  
          
                
//  Launch the request  
                Response response  =  client.put(itemUri, rep);  
                
return  response.getStatus().isSuccess();  
            }  
          
            
/**  
             * Try to delete an item. 
             * 
             * 
@param  client 
             *                the Restlet HTTP client. 
             * 
@param  itemUri 
             *                the resource's URI. 
             
*/   
            
public   static   boolean  deleteItem(Client client, Reference itemUri) {  
                
//  Launch the request  
                Response response  =  client.delete(itemUri);  
                
return  response.getStatus().isSuccess();  
            }  
}
复制代码

Hander:
  extends Object
Final handler of calls typcially created by Finders.Handler 的实例允许对处理的请求在一个线程安全的上下文

中运行.
-------------------------------------------------------------------------------------------------------
Uniform:
  extends Object
显露了统一的REST接口的基类.REST架构风格与其他基于网络的开发风格的重要特征是强调组件之间的一个统一接口。
-------------------------------------------------------------------------------------------------------
Resource:
  extends Handler
资源表示超连接资源目标中的概念
Intended conceptual target of a hypertext reference. "Any information that can be named can be a 
resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), 
-------------------------------------------------------------------------------------------------------
Variant:
资源的可能表现形式的描述
Descriptor for available representations of a resource. It contains all the important metadata about a 
representation but is not able to actually serve the representation's content itself.
--------------------------------------------------------------------------------------------------------
Representation:
表现用来展示资源(Resource)当前或即将的状态.
The content of a representataion can be retrieved several times if there is a stable and accessible 
source,like a local file or a string.When the representation is obtained via a temporary source like a 
network socket,
-------------------------------------------------------------------------------------------------------
Client:
 Connector acting as a generic client.[作为一个通用客户端的连接器].
-------------------------------------------------------------------------------------------------------
Reference:
 Reference to a URI.与Java中的URI类相比,这个接口表示互变的引用.
-------------------------------------------------------------------------------------------------------
Form:
 Form是一个专门的可修改的参数列表.
-------------------------------------------------------------------------------------------------------
Restlet:
 Restlet是一个提供上下文和生命周期支持的统一类。它有很多子类聚焦在采用特别方式对请求进行处理。
 The context property is typically provided by a parent Component as a way to encapsulate access to 

shared features such as logging and client connector.
-------------------------------------------------------------------------------------------------------
Application: 
 Restlet子类,可以被attached到一个或多个虚拟主机。
 Application同样也有很多有用的关系服务。
 @"connectorService" to declare necessary client and server connector.
 #"decoderService" to automatically decode or decompress request entities.
 #"metadataService" to provide access to metadata and their associated extension names.
 #"statusService" to provide common representations for exception status.
 # "taskService" to run tasks asynchronously.
 # "tunnelService" to tunnel method names or client preferences via query parameters.
-------------------------------------------------------------------------------------------------------
Component:
Restlet子类,用来管理一系列连接器、虚拟主机和应用程序。应用Application.Application are expected to be directly attached to VirtualHosts. Components are expose several services: access logging and status setting.
从软件架构的角度,组件是什么? 
Component is an abstract unit of software instruction and internal state that provides a tranformation 
of data via its interface"...
-------------------------------------------------------------------------------------------------------
Virtual Host: extends Router
将来自Server连接器的请求路由到Restlets. The attached Restlets are typically Application.
虚拟主机下的三个类:

Route attach(Restlet target)   
      Attaches a target Restlet to this router with an empty URI pattern.
Route attach(String uriPattern, Restlet target)
      Atttaches a target Restlet to this router based on a given URI pattern.
Route attachDefault(Restlet defaultTarget)


http://chatgpt.dhexx.cn/article/WwpYr8sg.shtml

相关文章

Help与Restlet组件之间的关系

Restlet 组件是组成 REST API 的模型&#xff0c;不同的模型中包含不同的属性和模型间的调用操作。 这些操作是为了完成更大粒度的组件的装配。 而 Help 是 REST API 与执行引擎之间的界面&#xff0c;或者说 Help 就是引擎用来执行操作的工具。 比如&#xff0c;在Component 中…

用Restlet创建面向资源的服务

Restlet项目&#xff08;http://www.restlet.org&#xff09;为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统&#xff0c;而不仅仅是REST式Web服务&#xff1b;而且&#xff0c;事实证明它自从2005年诞生之时起&#xf…

RESTlet——轻量级REST框架

https://github.com/restlet/restlet-framework-java https://restlet.talend.com/

ResT解读

最近的一篇基于Transformer的工作&#xff0c;由南京大学的研究者提出一种高效的视觉Transformer结构&#xff0c;设计思想类似ResNet&#xff0c;称为ResT&#xff0c;这是我个人觉得值得关注的一篇工作。 简介 ResT是一个高效的多尺度视觉Transformer结构&#xff0c;可以作…

Restlet入门示例

http://cunfu.iteye.com/blog/757467 本文的目的在于完成一个Restlet入门示例。 首先&#xff0c;是一个Stand alone应用。 然后&#xff0c;将其部署与Tomcat容器中。 最后&#xff0c;完成Restlet与Spring的整合。 1.按照官方教程&#xff0c;完成“firstSteps” 创建…

RestLet框架的入门

官网地址 创建maven工程&#xff08;war)pom.xml文件中导入jar包 <repositories><repository><id>restlet</id><url>http://maven.restlet.com/</url></repository></repositories><dependencies><dependency>&l…

Restlet

转载自http://my.oschina.net/javagg/blog/3254 关于本指南 本指南的翻译工作经过了Restlet社区的官方授权&#xff0c;cleverpig作为贡献者完成了本文的翻译和整理工作。在此发布Matrix社区试读版的目的是为了让更多的技术爱好者阅读并提出翻译中的不足之处&#xff0c;以提高…

restlet处理各种请求方式参考示例

restlet处理各种请求方式参考示例 1.新建web工程&#xff0c;项目结构如下&#xff1a; 1.编写实体类Student.java&#xff1a; package test;public class Student {private String name;private String sex;private int age;public Student(String name,String sex,int age){…

使用Restlet Client发送各种Get和Post请求

在开发web应用时&#xff0c;在对Spring中的Controller进行测试时&#xff0c;需要发送各种get以及post请求进行测试&#xff0c;当然可以自己在浏览器里输入url或者对于测试而言使用Spring提供的MockMvc编写代码进行测试&#xff0c;但是当我们想要测试诸如带Form表格提交&…

Restlet官方指南

Restlet官方指南 Table of contents Restlet overviewRetrieving the content of a Web pageListening to Web browsersOverview of a REST architectureComponents, virtual hosts and applicationsServing static filesAccess loggingDisplaying error pagesGuarding access …

谷歌安装Restlet Client插件

目录 一、Restlet Client插件下载链接二、Restlet Client插件安装步骤 一、Restlet Client插件下载链接 链接&#xff1a; https://pan.baidu.com/s/15rWwbQv6KlTS_T6tcpT_YA 提取码&#xff1a;6mxp 二、Restlet Client插件安装步骤 1、下载完Restlet-Client-v2.8.0.1.zip压…

Restlet指南

cleverpig 发表于 2007-11-30 15:15:48 作者:cleverpig 来源:Matrix 评论数:1 点击数:13,237 投票总得分:5 投票总人次:1 关键字:Restlet,REST,指南,入门 摘要: 当复杂核心化模式日趋强大之时&#xff0c;面向对象设计范例已经不总是Web开发中的最佳选择&#xff0c…

Restlet 学习笔记

摘要&#xff1a;网络上对 restlet 的评判褒贬不一&#xff0c;有的说框架封装的很好&#xff0c;很有弹性&#xff0c;有的说 rest 架构风格本身是一种简单的风格&#xff0c;restlet 过设计以使编程过于复杂&#xff0c;其实我倒不觉得 restlet 有什么复杂&#xff0c;相反很…

Restlet实战(一)Restlet入门资料及概念

先贴上几个本人认为比较有价值&#xff0c;值得初学者一看的文章。 http://www.matrix.org.cn/resource/article/2007-11-30/1312be72-9f14-11dc-bd16-451eadcf4db4.html http://blog.sina.com.cn/s/blog_537c5aab010096v8.html~typev5_one&labelrela_nextarticle http://…

Restlet 2.3 指南

2019独角兽企业重金招聘Python工程师标准>>> #Restlet 2.3 指南 #1. Restlet概述 Restlet框架由两个主要部分构成。首先&#xff0c;一部分是"Restlet API"&#xff0c;是一个中立的&#xff0c;支持REST概念&#xff0c;并能促进客户端、服务器端应用程序…

minio用法

1 Minio是在Apache License v2.0下发布的对象存储服务器。它与Amazon S3云存储服务兼容。 它最适合存储非结构化数据&#xff0c;如照片&#xff0c;视频&#xff0c;日志文件&#xff0c;备份和容器/ VM映像。对象的大小可以从几KB到最大5TB。 Minio服务器足够轻&#xff0c…

minio使用

一、介绍 开源协议的对象存储服务,轻量,性能强 二、安装 windows版链接: https://pan.baidu.com/s/1vv2p8bZBeZFG9cpIhDLVXQ?pwds5dd 提取码: s5dd 下载后创建minioData文件用于储存文件 创建run.bat脚本&#xff0c;内容如下 # 设置用户名 set MINIO_ROOT_USERadmin # …

CentOS Minimal 和 NetInstall 版本区别

Index of /centos/7.9.2009/isos/x86_64/ 如图&#xff1a; BinDVD版——就是普通安装版&#xff0c;需安装到计算机硬盘才能用&#xff0c;bin一般都比较大&#xff0c;而且包含大量的常用软件&#xff0c;安装时无需再在线下载&#xff08;大部分情况&#xff09;。 minim…

简易最小化虚拟机安装配置(CentOS-7-Minimal)

文章目录 镜像选择虚拟机安装&#xff08;VMware Workstation&#xff09;虚拟网络配置&#xff08;NAT模式&#xff09;虚拟网卡配置 虚拟机配置静态IP配置及测试系统初始化及库安装停止防火墙 & 关闭防火墙自启动关闭 selinux 防火墙更换镜像源并重建镜像源缓存安装 ifco…

pr双击打开图标没反应,下载ZXPSignLib-minimal.dll替换

微智启今天安装了pr cc2018&#xff0c;双击打开图标无反应 于是又试了Premiere cc2019&#xff0c;还是没反应 桌面还多出一些白色文件图标.crash结尾的 解决方案&#xff1a; 下载ZXPSignLib-minimal.dll文件&#xff0c;微智启软件工作室放到pr安装目录的根目录&#xff…