http://www.lifeba.org/arch/restlet_spring_3.html

前面两篇文章,我们介绍了基于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中配置,基本方法一样。这里不再阐述。

三、资源工程下载

RestSpringApplication工程

RESTLET开发实例(三)基于spring的REST服务相关推荐

  1. JAVA :RESTLET开发实例(一)基于JAX-RS的REST服务

    本文转自http://www.cnblogs.com/redmondfan/p/3209290.html,所有权力归原作者所有. RESTLET介绍 Restlet项目为"建立REST概念与 ...

  2. RESTLET开发实例(二)使用Component、Application的REST服务

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇文章,我们介绍了基于JAX-RS的REST服务,本篇文章我们介绍不基于JAX-RS的模式.JAX-RS其实就是一个简单的 ...

  3. 基于 Spring Cloud 的服务治理实践

    http://www.infoq.com/cn/articles/spring-cloud-based-service-governance 大家好,我是来自贝壳金控的赵文乐,目前主要从事架构方面的工 ...

  4. 贝壳金控赵文乐:基于 Spring Cloud 的服务治理实践

    大家好,我是来自贝壳金控的赵文乐,目前主要从事架构方面的工作.今天我想跟大家分享<基于 Spring Cloud 的服务治理实践>.我先简单向大家介绍一下服务治理的概念,然后介绍实际案例中 ...

  5. [转贴]JAVA:RESTLET开发实例(三)基于spring的REST服务

    前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将介绍restlet如何整合spring框架进行开发.Spring 是一个开源框架,是为了解决企业应 ...

  6. 基于Spring Boot垂钓服务系统的设计与实现毕业设计源码071739

    目  录 摘要 1 绪论 1.1 研究背景 1.2研究意义 1.3相关技术介绍 1.4论文结构与章节安排 2垂钓服务系统需求分析 2.1 可行性分析 2.1.1 技术可行性分析 2.1.2 经济可行性 ...

  7. 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现

    前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...

  8. opendds开发实例linux,基于OpenDDS的应用程序开发(2)公布端实现

    基于OpenDDS的应用程序开发 (2)公布端实现 本章主要介绍一个OpenDDS的应用程序的示例,演示如何开发一个将数据从公布端发送到订阅端的应用程序.该示例程序基于一个简单的消息应用程序,由一个简 ...

  9. python网站开发实例 flask_基于Python-Flask实现的网站例子

    使用说明 使用Flask制作的普通企业网站前台+后台 包括管理员登录( 主要是后台文章发布的功能的实现),注意:使用对象 是 flask的初学者,说明文档有详细的教程,包括包的引用 ,程序目录如何安排 ...

最新文章

  1. python库_计算机二级教程 Python语言程序设计,第10章python第三方库概览
  2. java.util.concurrent介绍
  3. VC++制作DLL具体解释
  4. 如履薄冰 —— Redis懒惰删除的巨大牺牲
  5. Java基础学习总结(142)——以正确的姿势使用Java 8 Optional
  6. canvas转化为图片并下载
  7. 集成学习 Bagging, Boosting, Stacking
  8. Linux 软件安装目录详解
  9. C#多线程之线程同步篇2
  10. 【python】打造一款自动扫描全网漏洞的扫描器
  11. 魔兽争霸修改器,局域网内使用!防封号!!!!!!!!
  12. unity 创建中文自定义字体
  13. 支付宝当面付扫码支付支付后不回调_【支付宝支付】支付宝手机网站支付流程...
  14. 罪恶都市中文java版_搜神录之罪恶都市BT版
  15. 大学生暑期实践活动---关注少数民族孤寡老人
  16. 前端清除缓存的集中方法
  17. 转载:APM文件夹释义
  18. 开放、数字化、创新、合作……华为云发力ing!
  19. Kubernetes单机开发环境部署记录
  20. 2021年中式烹调师(初级)模拟考试系统及中式烹调师(初级)实操考试视频

热门文章

  1. 大二暑假周进度报告(五)
  2. 阿里云linux下web服务器配置
  3. MapReduce实例(数据去重)
  4. 在word中,整篇文章想要在每一章另起一页
  5. C#中模态对话框释放问题
  6. C语言基础MessageBox(......)用法详解
  7. 在C#中使用SerialPort类实现串口通信 遇到多线程问题
  8. vue npm: react运行时报错npm start — babel-eslint 版本兼容性问题
  9. linux中mount -o remount /home,mount -o remount, rw / 很有用..
  10. NCSDK make install: Error in line 170