- 浏览: 153507 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
守望麦穗:
楼主好, 我按着你的步骤查找问题 ,到第二步,也没有自定义U ...
Spring Security3边学边写(N)会话管理和并行控制 -
sb33060418:
Notify 写道楼主,但是配置<concurrency ...
Spring Security3边学边写(N)会话管理和并行控制 -
Notify:
楼主,但是配置<concurrency-control ...
Spring Security3边学边写(N)会话管理和并行控制 -
409421884:
你好,我也在做这个功能,但sessionRegistry这个东 ...
Spring Security3边学边写(N)会话管理和并行控制 -
sb33060418:
左眼的彼岸 写道sb33060418 写道左眼的彼岸 写道谢谢 ...
Spring Security3边学边写(N)会话管理和并行控制
上一次实践是使用Component来发布多个Resource。本次实践将Restlet与spring集成,并使用spring来配置、发布、管理多个Resource。
参考:http://ajaxcn.iteye.com/blog/416913
本次实践需要导入spring相关包,创建配置文件applicationContext.xml并进行配置。
1.导入spring相关包
将Restlet安装目录\Edition Java EE\2.0.10\lib下的org.restlet.ext.spring.jar、net.sf.cglib.jar 两个包和spring.jar包加入Build Path,我使用的是spring2.0。
2.Dao
在com.sunny.restlet.order目录下创建OrderDao接口,代码如下:
接口中定义数据访问方法。
在com.sunny.restlet.order目录下创建OrderDaoImpl类,代码如下:
类实现了OrderDao接口。
3.Resource
修改com.sunny.restlet.order.CustomerResource类,代码如下:
修改后的类相当于spring中的service bean,类中调用了orderDao的数据访问接口。
修改com.sunny.restlet.order.OrderResource类,代码如下:
4.applicationContext.xml
在src/下创建spring配置文件applicationContext.xml,代码如下:
文件中定义了component、router、resource和dao,以及将Resource发布到的路径。使用spring不需要再自定义Component和Application。
5.web.xml
修改web.xml文件,删除原有Restlet配置,加入spring配置,代码如下:
文件中定义spring配置文件位置,指定使用SpringServerServlet来提供服务,并定义了在spring配置中Component Bean的id(component)。
6.运行
重新部署后,通过浏览器访问http://localhost:8080/firstSteps/spring/customers/1可以看到提示信息
访问http://localhost:8080/firstSteps/spring/orders/1/2可以看到提示信息
说明Restlet和spring集成成功。
7.SpringRouter
SpringBeanRouter要求resource继承ServerResource类,自动将spring BeanFactory中name以"/"开头的bean加到路径上。
除此外还可以使用SpringRouter来集成,applicationContext.xml代码如下:
SpringRouter调用SpringFinder,需要指定lookup-method,如果resource继承自Resource,则name="createResource",且resource要实现无参构造函数和init()方法;如果resource继承自ServerResource,则name="create"。详情请参考Restlet源码,SpringFinder类中的createTarget(Request request, Response response)、createResource()和create()方法,SpringBeanFinder类中的create()方法。
8.RestletFrameworkServlet
还可以使用RestletFrameworkServlet替代SpringServerServlet。这需要导入spring-webmvc.jar包。
修改web.xml,代码如下:
代码中将targetRestletBeanName这个init-param的值设定为restRouter,如果不设置,默认为root。RestletFrameworkServlet根据这个参数的值从WEB-INF\restlet-servlet.xml中读取配置。
在WEB-INF目录下创建restlet-servlet.xml,代码如下:
如果applicationContext.xml放到scr目录下,
web.xml这样写
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
浪费了我好长时间
参考:http://ajaxcn.iteye.com/blog/416913
本次实践需要导入spring相关包,创建配置文件applicationContext.xml并进行配置。
1.导入spring相关包
将Restlet安装目录\Edition Java EE\2.0.10\lib下的org.restlet.ext.spring.jar、net.sf.cglib.jar 两个包和spring.jar包加入Build Path,我使用的是spring2.0。
2.Dao
在com.sunny.restlet.order目录下创建OrderDao接口,代码如下:
package com.sunny.restlet.order; public interface OrderDao { public String getOrderById(String orderId); public String getSubOrderById(String subOrderId); public String getCustomerById(String custId); }
接口中定义数据访问方法。
在com.sunny.restlet.order目录下创建OrderDaoImpl类,代码如下:
package com.sunny.restlet.order; public class OrderDaoImpl implements OrderDao { public String getCustomerById(String custId) { // TODO Auto-generated method stub return "customer" + custId; } public String getOrderById(String orderId) { // TODO Auto-generated method stub return "order" + orderId; } public String getSubOrderById(String subOrderId) { // TODO Auto-generated method stub return "subOrder" + subOrderId; } }
类实现了OrderDao接口。
3.Resource
修改com.sunny.restlet.order.CustomerResource类,代码如下:
package com.sunny.restlet.order; import org.restlet.resource.Get; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; public class CustomerResource extends ServerResource { String customerId = ""; private OrderDao orderDao; @Override protected void doInit() throws ResourceException { this.customerId = (String) getRequest().getAttributes().get("custId"); } @Get public String represent() { return "get customer id :" + orderDao.getCustomerById(customerId) ; } public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } }
修改后的类相当于spring中的service bean,类中调用了orderDao的数据访问接口。
修改com.sunny.restlet.order.OrderResource类,代码如下:
package com.sunny.restlet.order; import org.restlet.resource.Get; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; public class OrderResource extends ServerResource { String orderId = ""; String subOrderId = ""; private OrderDao orderDao; @Override protected void doInit() throws ResourceException { this.orderId = (String) getRequest().getAttributes().get("orderId"); this.subOrderId = (String) getRequest().getAttributes().get( "subOrderId"); } @Get public String represent() { return "the order id is : " + orderDao.getOrderById(orderId) + " and the sub order id is : " + orderDao.getSubOrderById(subOrderId); } public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } }
4.applicationContext.xml
在src/下创建spring配置文件applicationContext.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- component --> <bean id="component" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="restRouter" /> </bean> <!-- router --> <bean id="restRouter" class="org.restlet.ext.spring.SpringBeanRouter"> </bean> <!-- resource --> <bean name="/customers/{custId}" id="customerResrouce" class="com.sunny.restlet.order.CustomerResource" /> <bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce" class="com.sunny.restlet.order.OrderResource" /> <!-- dao --> <bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" /> </beans>
文件中定义了component、router、resource和dao,以及将Resource发布到的路径。使用spring不需要再自定义Component和Application。
5.web.xml
修改web.xml文件,删除原有Restlet配置,加入spring配置,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- SpringServerServlet --> <servlet> <servlet-name>restlet</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> <servlet-mapping> <servlet-name>restlet</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> <!-- welcome-file-list --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
文件中定义spring配置文件位置,指定使用SpringServerServlet来提供服务,并定义了在spring配置中Component Bean的id(component)。
6.运行
重新部署后,通过浏览器访问http://localhost:8080/firstSteps/spring/customers/1可以看到提示信息
- get customer id :customer1
访问http://localhost:8080/firstSteps/spring/orders/1/2可以看到提示信息
- the order id is : order1 and the sub order id is : subOrder2
说明Restlet和spring集成成功。
7.SpringRouter
SpringBeanRouter要求resource继承ServerResource类,自动将spring BeanFactory中name以"/"开头的bean加到路径上。
除此外还可以使用SpringRouter来集成,applicationContext.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- component --> <bean id="component" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="restRouter" /> </bean> <!-- router --> <bean id="restRouter" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/customers/{custId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="create" bean="customerResource" /> </bean> </entry> <entry key="/orders/{orderId}/{subOrderId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="create" bean="orderResource" /> </bean> </entry> </map> </property> </bean> <!-- resource --> <bean id="customerResource" class="com.sunny.restlet.order.CustomerResource"> </bean> <bean id="orderResource" class="com.sunny.restlet.order.OrderResource"> </bean> <!-- dao --> <bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" /> </beans>
SpringRouter调用SpringFinder,需要指定lookup-method,如果resource继承自Resource,则name="createResource",且resource要实现无参构造函数和init()方法;如果resource继承自ServerResource,则name="create"。详情请参考Restlet源码,SpringFinder类中的createTarget(Request request, Response response)、createResource()和create()方法,SpringBeanFinder类中的create()方法。
8.RestletFrameworkServlet
还可以使用RestletFrameworkServlet替代SpringServerServlet。这需要导入spring-webmvc.jar包。
修改web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- SpringServerServlet --> <servlet> <servlet-name>restlet</servlet-name> <servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class> <init-param> <param-name>targetRestletBeanName</param-name> <param-value>restRouter</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>restlet</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> <!-- welcome-file-list --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
代码中将targetRestletBeanName这个init-param的值设定为restRouter,如果不设置,默认为root。RestletFrameworkServlet根据这个参数的值从WEB-INF\restlet-servlet.xml中读取配置。
在WEB-INF目录下创建restlet-servlet.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- router --> <bean id="restRouter" class="org.restlet.ext.spring.SpringBeanRouter"> </bean> <!-- resource --> <bean name="/customers/{custId}" id="customerResrouce" class="com.sunny.restlet.order.CustomerResource" /> <bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce" class="com.sunny.restlet.order.OrderResource" /> <!-- dao --> <bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" /> </beans>
评论
2 楼
xiaoqin_007
2013-06-14
好文
博主能否写一篇整合 Restlet+Spring+JSR311的文章?
现在路径对应方式太死板了 (只能在Bean里面对应,且类中只能使用CRUD四个方法)。


博主能否写一篇整合 Restlet+Spring+JSR311的文章?
现在路径对应方式太死板了 (只能在Bean里面对应,且类中只能使用CRUD四个方法)。
1 楼
gaofeng_867
2013-02-20
如果applicationContext.xml放到scr目录下,
web.xml这样写
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
浪费了我好长时间
发表评论
-
REST(五)CXF实现REST
2017-08-14 16:49 783Apache CXF以前一般用来开发基于SOAP协议的Web ... -
REST(四)RESTEasy实现REST
2017-06-15 10:55 3781RESTEasy是JBoss的一个开源项目,提供各种框架帮助你 ... -
REST(三)Restlet实现REST
2017-06-12 17:13 2485Restlet项目为“建立REST概念与Java类之间的映射” ... -
REST(二)Jersey实现REST
2017-05-26 16:33 2016Jersey是JAX-RS(JSR311)开源参考实现,用于构 ... -
REST(一)REST和JAX-RS
2017-05-25 16:08 1416最近重新整理了一下代码,把java实现REST api的几种框 ... -
Restlet 2.0 边学边写(八)使用jQuery和ajax实现对Restlet资源的CRUD操作
2013-01-12 01:20 3002上一次实践实现了各方 ... -
Restlet 2.0 边学边写(七)Restlet返回xml和json数据格式
2013-01-07 10:29 5381上一次实践实现了html form来访问Restlet的PUT ... -
Restlet 2.0 边学边写(六)使用html form访问Restlet的PUT和DELETE
2013-01-04 15:55 4020上一次实践实现了POST、PUT和DELETE方法,并使用ht ... -
Restlet 2.0 边学边写(五)Restlet的POST、PUT和DELETE
2012-12-26 17:54 4812上一次实践是将Restlet与spring集成,本次实践是将实 ... -
Restlet 2.0 边学边写(三)使用Component发布多个Application
2012-12-19 18:25 3195很久没更新这篇博客了,今天继续。 上一次实践是一个Appli ... -
Restlet 2.0 边学边写(二)发布多个Resource
2012-01-11 03:39 3630上一次实践是一个Application绑定一个Resource ... -
Restlet 入门
2012-01-11 03:06 2587我学习Restlet是从ajax写的Restlet实践系列博客 ... -
Restlet 2.0 边学边写(一)第一步
2012-01-11 02:42 7600关于Rest的起源和框架、入门资料的一些东西大家可以去看看aj ...
相关推荐
在这个场景中,我们关注的是"restlet2.0+spring3.0+hibernate3.3"的整合,这是一个经典的Java Web开发组合,分别代表了RESTful API、服务层管理和持久化层的优秀实践。 首先,让我们深入了解每个框架的核心特性: ...
"reslet2.0+spring3.0+hibernate3.3框架集成" 这个标题表明这是一个关于Java开发中的技术整合项目。Reslet 2.0是一个轻量级的REST(Representational State Transfer)应用框架,用于构建Web服务和应用程序。Spring ...
总的来说,RESTlet自用2可能是一个基于RESTlet 2.0版本并结合Spring框架的个人项目,该项目可能涉及到了RESTful服务的开发、JAX-RS标准的实现以及Spring的集成。相关的学习资料,如"MHT"文件和"PPT",提供了理论知识...
这个花了一点时时间做出来的简单的集成RSH框架,喜欢restlet的朋友可以研究研究,大家一起讨论一下,最简单的一个流程,已经完成了,个人感觉比ssh框架开发简单,中间都没有对象new,都通过spring注入方式获取对象,...
4. **高级主题**:涵盖如安全、性能优化、与其他技术(如JPA、Spring)集成等高级话题,有助于开发者解决实际问题。 5. **案例研究**:文档可能包含了一些示例应用,展示如何在不同场景下使用Restlet,包括创建复杂...