前面两篇文章,我们介绍了基于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工程

转载于:https://www.cnblogs.com/redmondfan/p/3209294.html

[转贴]JAVA: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. java项目-第99期基于spring+springmvc+hibernate的在线问卷答题系统-计算机毕业设计

    java项目-第99期基于spring+springmvc+hibernate的在线问卷答题系统[毕业设计] [源码请到下载专栏下载] 1.项目简述 今天分享的项目是<在线问卷答题系统>, ...

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

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

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

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

  6. RESTLET开发实例(三)基于spring的REST服务

    http://www.lifeba.org/arch/restlet_spring_3.html 前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将 ...

  7. Kotlin和Java混合开发总结(基于Kotlin1.3.0版本)

    我本来是不想学习kotlin的,但是现在的形势,很多公司都在使用kotlin开发,可以说学会kotlin也是无奈之举,既然是潮流,谷歌也在大力推广,所以还是只能硬着头皮逼迫自己学一下,也能更快适应公司 ...

  8. java游戏开发毕业论文_基于JAVA的五子游戏的开发设计毕业论文

    基于JAVA的五子游戏的开发设计毕业论文 毕业论文基于 JAVA 的五子游戏的开发设计山东凯文科技职业学院摘 要人工智能是一门正在迅速发展的新兴的,综合性很强的交叉科学.它与生物工程.空间技术一起被并 ...

  9. 【Java后台】从零开始的Java后台开发(三)

    编写基础的Servlet应用程序 1 Servlet 使用Servlet需要在pom.xml中引入以下依赖: <!-- https://mvnrepository.com/artifact/ja ...

最新文章

  1. pycharm中的requirements.txt文件—如何生成及导入
  2. 综合布线施工中的不规范现象
  3. Linux系统的磁盘管理
  4. python itchat库安装_操作微信-itchat库的安装
  5. 洛谷 - P4390 [BOI2007]Mokia 摩基亚(带修二维数点-四叉线段树/CDQ分治)
  6. 蓄电池单格电压多少伏_蓄电池充电规范手册
  7. .net md5 java 偏移量_C# java MD5加密方不一致问题
  8. 基于Java+SpringMvc+vue+element实现高效学生社团平台管理
  9. 值传递与引用传递 c# 1613535799
  10. Android 5.0有哪些变化
  11. 不刷新页面的tab_现代 Web 页面开发流程
  12. python flask服务器假死_python – Flask POST请求导致服务器崩溃
  13. 服务器配置多个域名冲突
  14. NYOJ-111分数加减法
  15. 网线传输速度测试_教你局域网中怎样对网线进行全面测试
  16. 硬盘分区被格式化了如何恢复
  17. G. 打印 LOGO(递归)
  18. uva11045(最大二分图匹配)
  19. 复合梯形的matlab求解,MATLAB 利用复合梯形公式求解积分
  20. 模型实践 | AI驱动的全球精准气象预报

热门文章

  1. openmp与openmpi区别
  2. leetcode —— 783. 二叉搜索树结点最小距离
  3. SQL避免IN 和 NOT IN
  4. pandas.describe()参数的意义
  5. F. 张胖胖玩多米诺骨牌 (南阳理工oj—21新生第二场招新赛)
  6. Vmware 连接局域网通过桥接方式
  7. python颜色表_数据库表连接的简单解释 | 图文并茂,通俗易懂
  8. python rabitmq_python使用rabbitmq实例二,工作队列
  9. php 解包二进制,workerman的二进制怎么玩啊,怎么封包,怎么解包啊
  10. CentOS7安装oracle11G数据库