RESTLET开发实例(三)基于spring的REST服务
前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务。这里将介绍restlet如何整合spring框架进行开发。Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的,广泛的应用于应用项目中。本篇文章将在上篇的基础上面做介绍,将上篇文章的介绍的rest,用spring整合来实现。
一、基于spring配置的Rest简单服务
1、新建RestSpringApplication Web工程。

将restlet和spring的jar包复制进来。红色部分为新加入进来的jar包。

将上篇中的RestApplication工程项目中的src的源文件复制过来。
2、将web.xml加入下面代码
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>
org.restlet.ext.spring.RestletFrameworkServlet
</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>restletComponent</param-value>
</init-param>
</servlet><servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
上面代码指定了restlet使用spring的RestletFrameworkServlet。
3、建立restlet-servlet.xml文件,只需要配置org.restlet.ext.spring.SpringRouter,及对应的路径和资源文件。
<bean name="root">
<property name="attachments">
<map>
<entry key="/student/{studentId}">
<bean>
<lookup-method name="create" bean="StudentResource" />
</bean>
</entry>
<entry key="/student">
<bean>
<lookup-method name="create" bean="StudentsResource" />
</bean>
</entry></map>
</property>
</bean><bean id="StudentResource" class="org.lifeba.ws.resource.StudentResource" scope="prototype"/>
<bean id="StudentsResource" class="org.lifeba.ws.resource.StudentsResource" scope="prototype"/>
上面的代码配置了
/student/{studentId}对应StudentResource,以及student对应StudentsResource资源类。通过
SpringRouter可以非常方便的通过attachments配置资源路径。如果你有更多的路径,你可以建立多个entry即可。
4、好了,你现在可以重启tomcat了,输入 http://localhost:8085/RestSpringApplication/student/1,访问正常。

二、测试添加、删除、更新方法。
1、student_post方法,添加一个Student,成功后返回新建studentId为2的对象。
public void student_post(){
try {
Form queryForm = new Form();
queryForm.add("name","steven_spring");
queryForm.add("clsId","201002");
queryForm.add("sex","2");
queryForm.add("age","12");
ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student");
Representation representation =client.post(queryForm.getWebRepresentation());
System.out.println(representation.getText());
} catch (Exception e) {e.printStackTrace();
}
}
访问http://localhost:8085/RestSpringApplication/student/2

访问http://localhost:8085/RestSpringApplication/student ,可以看到有2个Student对象。

2、student_delete方法,删除Id为1的Student,成功执行后返回1。
public void student_delete(){
try {
ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student/1");
Representation representation =client.delete();
System.out.println(representation.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
再次访问http://localhost:8085/RestSpringApplication/student,可以看到只有一个Id为2的Student对象。

3、student_put方法,更新Id为2的Student。
public void student_put(){
try {
Form queryForm = new Form();
queryForm.add("name","steven_spring_modify");
queryForm.add("clsId","201012");
queryForm.add("sex","12");
queryForm.add("age","24");ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student/2");
Representation representation =client.put(queryForm);
System.out.println(representation.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
访问http://localhost:8085/RestSpringApplication/student/2

通过上面的代码已经完全实现了Spring中的restlet的配置。上面只对Student对象做了介绍,你也可以实现对Course在spring中配置,基本方法一样。这里不再阐述。
三、资源工程下载
RESTLET开发实例(三)基于spring的REST服务的更多相关文章
- RESTLET开发实例(一)基于JAX-RS的REST服务
RESTLET介绍 Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架.它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务. Restlet项 ...
- [转贴]JAVA :RESTLET开发实例(一)基于JAX-RS的REST服务
RESTLET介绍 Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架.它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务. Restlet项 ...
- [转贴]JAVA:RESTLET开发实例(二)使用Component、Application的REST服务
上一篇文章,我们介绍了基于JAX-RS的REST服务,本篇文章我们介绍不基于JAX-RS的模式.JAX-RS其实就是一个简单的 Application服务.和我们接下来介绍的Application基本 ...
- [转贴]JAVA:RESTLET开发实例(三)基于spring的REST服务
前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将介绍restlet如何整合spring框架进行开发.Spring 是一个开源框架,是为了解决企业应 ...
- 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现
前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(三)
属性配置 1.配置application.properties文件 配置web访问端口和context path server.port = 8081 server.servlet.context-p ...
- RESTLET开发实例
1 前提 由于近期工作的需要,要把RESTLET应用到项目中,于是在网上参考了一些资料的基础上,实践了一个关于RESTLET接口的小例子. Restlet的思想是:HTTP客户端与HTTP服务器之间的 ...
- RRiBbit,一个事件总线.基于spring配置不同服务通信!
1.何为RRiBbit? *一种开源事件总线技术,能够让模块(组件)之间双向通讯,也被称为请求相应总线(request-response-bus),使用简单,相对其他框架来说,RRiBbit只需要加个 ...
- Spring cloud微服务安全实战_汇总
Spring cloud微服务安全实战 https://coding.imooc.com/class/chapter/379.html#Anchor Spring Cloud微服务安全实战-1-1 课 ...
随机推荐
- PHP连接sqlserver的两种方法,向sqlserver2000中写入数据,中文乱码
项目环境是php5.3.28 项目用的ThinkPHP3.2.3 已经mysql5.5数据库,要和另一个项目对接,需要连接sqlsever2000数据库进行一些操作. 第一种用php自带扩展连接数据 ...
- Sublime Text 3 配置浏览器预览路径 localhost
原文链接:http://jingyan.baidu.com/article/15622f2419ce79fdfcbea5ea.html 按步骤设置成功!感谢大侠! 以下是原文: Sublime Tex ...
- python使用mysqldb连接数据库操作方法示例详解
这篇文章主要介绍了python mysqldb使用方法,大家参考使用 复制代码代码如下: # -*- coding: utf-8 -*- #mysqldb # site www.jbxue.com ...
- SET QUOTED_IDENTIFIER ON
语法 SET QUOTED_IDENTIFIER { ON | OFF } 注释 当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔 ...
- mcollective安装过程
参考 http://kisspuppet.com/2013/11/10/mcollective-middleware/ http://5lexin.com/blog/view/225/mco-ping ...
- poj 2187 Beauty Contest
Beauty Contest 题意:给你一个数据范围在2~5e4范围内的横纵坐标在-1e4~1e4的点,问你任意两点之间的距离的最大值的平方等于多少? 一道卡壳凸包的模板题,也是第一次写计算几何的题, ...
- __sync_fetch_and_add
最近在公司离职的前辈写的代码哪里看到了__sync_fetch_and_add这个东东.比较好奇.找些资料学习学习 http://www.lxway.com/4091061956.htm http:/ ...
- MongoDB 权限认证
MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),因为考虑到数据安全的原因特地花了一点时间研究了一下,网上搜出来的解 ...
- BZOJ 3998 [TJOI 2015] 弦论 解题报告
这是一道后缀自动机经典题目. 对于 $t=0$ 的情况:每个节点都代表一个子串,所以我们给每个节点的 $Size$ 都记为 $1$, 对于 $t=1$ 的情况:我们只给 $last$ 节点的 $Siz ...
- 使用flask的时候遇到的问题及其解答
在网上看到了mircoblog的这个web程序,用flask框架写的,自己就在windows的环境下实现了下. 1.这个博客系统用到了一个flask插件叫flask_Login 里面涉及到编码解码的问 ...