Restlet入门示例

article/2025/9/21 9:24:14

http://cunfu.iteye.com/blog/757467

本文的目的在于完成一个Restlet入门示例。
首先,是一个Stand alone应用。
然后,将其部署与Tomcat容器中。
最后,完成Restlet与Spring的整合。

1.按照官方教程,完成“firstSteps”

创建一个动态Web工程RestEE,并建立firstSteps包,并复制如下代码到:HelloWorldResource.java

Java代码 复制代码  收藏代码
  1. package firstSteps;   
  2.   
  3. import org.restlet.Context;   
  4. import org.restlet.Request;   
  5. import org.restlet.Response;   
  6. import org.restlet.resource.Get;   
  7. import org.restlet.resource.ServerResource;   
  8.   
  9. /**  
  10.  * Resource which has only one representation.  
  11.  */  
  12. public class HelloWorldResource extends ServerResource {   
  13.   
  14.     @Get  
  15.     public String represent() {   
  16.         return "hello, world";   
  17.     }   
  18.   
  19. }  
package firstSteps;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
/**
* Resource which has only one representation.
*/
public class HelloWorldResource extends ServerResource {
@Get
public String represent() {
return "hello, world";
}
}



2.复制如下代码到FirstStepsApplication.java

Java代码 复制代码  收藏代码
  1. package firstSteps;   
  2.   
  3. import org.restlet.Application;   
  4. import org.restlet.Restlet;   
  5. import org.restlet.routing.Router;   
  6.   
  7. public class FirstStepsApplication extends Application {   
  8.   
  9.     /**  
  10.      * Creates a root Restlet that will receive all incoming calls.  
  11.      */  
  12.     @Override  
  13.     public synchronized Restlet createInboundRoot() {   
  14.         // Create a router Restlet that routes each call to a new instance of HelloWorldResource.   
  15.         Router router = new Router(getContext());   
  16.   
  17.         // Defines only one route   
  18.         router.attach("/hello", HelloWorldResource.class);   
  19.   
  20.         return router;   
  21.     }   
  22.   
  23. }  
package firstSteps;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class FirstStepsApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attach("/hello", HelloWorldResource.class);
return router;
}
}


3.编写main,复制如下代码到:FirstStepsMain.java

Java代码 复制代码  收藏代码
  1. package firstSteps;   
  2.   
  3. import org.restlet.Component;   
  4. import org.restlet.data.Protocol;   
  5.   
  6. public class FirtstStepsMain {   
  7.     public static void main(String[] args) throws Exception {     
  8.         // Create a new Component.     
  9.         Component component = new Component();     
  10.          
  11.         // Add a new HTTP server listening on port 8182.     
  12.         component.getServers().add(Protocol.HTTP, 8182);     
  13.          
  14.         // Attach the sample application.     
  15.         component.getDefaultHost().attach("/firstSteps",     
  16.                 new FirstStepsApplication());     
  17.          
  18.         // Start the component.     
  19.         component.start();     
  20.     }    
  21. }  
package firstSteps;
import org.restlet.Component;
import org.restlet.data.Protocol;
public class FirtstStepsMain {
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("/firstSteps",  
new FirstStepsApplication());  
// Start the component.  
component.start();  
} 
}



4.以Java Application运行FirstStepsMain,在浏览器中输入:
http://localhost:8182/hello,将打印“hello, world”信息。

5.修改web.xml,具体代码如下:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.     
  4.    <display-name>firstSteps</display-name>  
  5.    <context-param>     
  6.       <param-name>org.restlet.application</param-name>     
  7.       <param-value>     
  8.          firstSteps.FirstStepsApplication   
  9.       </param-value>     
  10.    </context-param>  
  11.    <!-- Restlet adapter -->     
  12.    <servlet>     
  13.       <servlet-name>RestletServlet</servlet-name>    
  14.       <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>  
  15.          
  16.          
  17.    </servlet>     
  18.      
  19.    <!-- Catch all requests -->     
  20.    <servlet-mapping>     
  21.       <servlet-name>RestletServlet</servlet-name>     
  22.       <url-pattern>/*</url-pattern>     
  23.    </servlet-mapping>     
  24. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>firstSteps</display-name>
<context-param>  
<param-name>org.restlet.application</param-name>  
<param-value>  
firstSteps.FirstStepsApplication
</param-value>  
</context-param>
<!-- Restlet adapter -->  
<servlet>  
<servlet-name>RestletServlet</servlet-name> 
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>  
<!-- Catch all requests -->  
<servlet-mapping>  
<servlet-name>RestletServlet</servlet-name>  
<url-pattern>/*</url-pattern>  
</servlet-mapping>  
</web-app>



6.启动Tomcat,运行该项目。在浏览器中输入:
http://localhost:8080/RestEE/hello,出现“hello, world”信息。

7.集成restlet,spring
7.1 修改web.xml,内容如下:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.     
  4.    <display-name>firststepsservlet</display-name>  
  5.          
  6.    <context-param>     
  7.       <param-name>contextConfigLocation</param-name>     
  8.       <param-value>/WEB-INF/applicationContext.xml</param-value>     
  9.    </context-param>     
  10.        
  11.    <listener>     
  12.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
  13.    </listener>     
  14.       
  15.    <!-- Restlet adapter -->     
  16.     <servlet>     
  17.         <servlet-name>RestletServlet</servlet-name>     
  18.         <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>  
  19.         <init-param>  
  20.             <param-name>org.restlet.component</param-name>      
  21.             <param-value>component</param-value>      
  22.         </init-param>  
  23.     </servlet>     
  24.      
  25.    <!-- Catch all requests -->     
  26.    <servlet-mapping>     
  27.       <servlet-name>RestletServlet</servlet-name>     
  28.       <url-pattern>/*</url-pattern>     
  29.    </servlet-mapping>     
  30. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>firststepsservlet</display-name>
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>/WEB-INF/applicationContext.xml</param-value>  
</context-param>  
<listener>  
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<!-- Restlet adapter -->  
<servlet>  
<servlet-name>RestletServlet</servlet-name>  
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>   
<param-value>component</param-value>   
</init-param>
</servlet>  
<!-- Catch all requests -->  
<servlet-mapping>  
<servlet-name>RestletServlet</servlet-name>  
<url-pattern>/*</url-pattern>  
</servlet-mapping>  
</web-app>



7.2 application.xml内容如下:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     
  6.     <bean id="component" class="org.restlet.ext.spring.SpringComponent">  
  7.         <property name="defaultTarget" ref="application" />    
  8.     </bean>  
  9.     <bean id="application" class="firstSteps.FirstStepsApplication">  
  10.         <lookup-method name="createRoot" bean="root" />  
  11.     </bean>  
  12.     <bean id="root" class="org.restlet.ext.spring.SpringRouter">  
  13.         <property name="attachments">  
  14.             <map>  
  15.                 <entry key="/hello">  
  16.                     <bean class="org.restlet.ext.spring.SpringFinder">  
  17.                         <lookup-method name="create" bean="HelloWorldResource" />  
  18.                     </bean>  
  19.                 </entry>  
  20.             </map>  
  21.         </property>  
  22.     </bean>  
  23.     <bean id="HelloWorldResource" class="firstSteps.HelloWorldResource" scope="prototype" />  
  24. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="component" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="application" /> 
</bean>
<bean id="application" class="firstSteps.FirstStepsApplication">
<lookup-method name="createRoot" bean="root" />
</bean>
<bean id="root" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/hello">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="HelloWorldResource" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="HelloWorldResource" class="firstSteps.HelloWorldResource" scope="prototype" />
</beans>



7.3 启动Tomcat,运行该项目。浏览器中输入:
http://localhost:8080/RestEE/hello,出现“hello,world”信息。


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

相关文章

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…

Minimal Square

文章目录 一、A. Minimal Square总结 一、A. Minimal Square 本题链接&#xff1a;A. Minimal Square 题目&#xff1a; A. Minimal Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Find the minimu…

出现minimal bash-like...的问题如何解决?

2021.9.4写下此文&#xff0c;以备查阅。 问题如图&#xff1a; 一般出现这个界面即为引导程序出现问题&#xff0c;根据下面两种情况看待&#xff1a; 卸载双系统之一&#xff08;比如之前是windeepin双系统&#xff0c;现在卸载了deepin系统&#xff09;重启时出现。安装新…

Centos教程,DVD、Everything、Minimal、NetInstall区别

今天给大家讲述一下在官网下载Linux中Centos7的时候遇到的版本问题。首先给大家简述一下Centos下载流程。 1.百度搜索Centos&#xff0c;点击官网。 2.点击Download&#xff0c;选择Centos7&#xff08;举例&#xff09;。 3.然后这里我们选择aliyun下载。 4.选择第一个镜像版本…

【已解决】grub引导项修复:Minimal BASH-like line editing is supported.

目录 1 问题背景2 问题探索3 问题解决4 告别Bug 1 问题背景 环境&#xff1a; Win10Ubuntu20.04 现象&#xff1a;双系统电脑向移动硬盘安装Ubuntu系统后&#xff0c;重启黑屏并显示Minimal BASH-like line editing is supported. For the first word, TAB lists possible comm…

Centos7 Minimal 版本基本配置记录

每次搭测试环境之前都需要先装一台干净的虚拟机&#xff0c;然而 Centos7 Minimal 版本快速装完之后还需要配置&#xff1a;网络、国内源、一些基础工具&#xff08;net-tools、vim&#xff09;等才能远程连接和使用。记录一下&#xff0c;方便下次快速配置使用。 目录 1、网…