- 浏览: 251988 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
fjdingsd:
目前基于REST的Java框架不包括Jersey吗
Hello REST!!! -
qq690388648:
不错,说的很好!
Restlet实战(十四)如何在Restlet得到Servlet request和Session -
zhuanbiandejijie:
唉... 你09年就接触Restlet了.15年我才开始看Re ...
Hello REST!!! -
zmjiao:
client.options( 这个是那个包下面的? rest ...
Restlet实战(十八)Restlet如何产生WADL -
shihezichen:
对于最近很多人都在讨论的, 使用REST时就不应该掺杂事务的看 ...
Restlet实战(二十六)事务 (Transaction)
在上一篇文章中介绍了如何在restlet.xml中设置Component,本篇将介绍restlet如何和Spring结合。
首先将相应的jar文件放到WEB-INF/lib下,针对上一篇的示例代码,我们做一些修改,当然也包括一些配置。
首先
在web.xml注释掉如下代码:
<servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class> com.noelios.restlet.ext.servlet.ServerServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
因为有专门的SpringServlet来处理请求,所以,加入如下代码:
<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> <servlet> <servlet-name>restlet</servlet-name> <servlet-class>com.noelios.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>/resources/*</url-pattern> </servlet-mapping>
接下来对CustomerResource做一修改,值得注意的是与Spring集成,Resource类里面有一些规则,首先必须有一个无参的构造参数,一个init方法,这个方法包含那些原来定义在非默认构造函数的代码。
public class CustomerResource extends Resource { String customerId = ""; private CustomerDAO customerDAO; @Override public void init(Context context, Request request, Response response) { super.init(context, request, response); customerId = (String) request.getAttributes().get("custId"); } public CustomerResource(){ getVariants().add(new Variant(MediaType.TEXT_PLAIN)); } public CustomerResource(Context context, Request request, Response response) { super(context, request, response); getVariants().add(new Variant(MediaType.TEXT_PLAIN)); } @Override public Representation getRepresentation(Variant variant) { String userMsg = customerDAO.getCustomerById(customerId); Representation representation = new StringRepresentation(userMsg, MediaType.TEXT_PLAIN); return representation; } public void setCustomerDAO(CustomerDAO customerDAO) { this.customerDAO = customerDAO; } }
需要说明的是按照现在三层架构流行分法,我们姑且认为Resource就是我们常说的Service层,那么在resource里面我们需要调用数据层的类来完成对数据的处理。上面代码中,我创建了一个CustomerDAO作为数据处理层,相关类代码如下:
public interface CustomerDAO { public String getCustomerById(String id); }
public class CustomerDAOImpl implements CustomerDAO{ public String getCustomerById(String id){ //get other information through id such as name, no, address etc. String name = "ajax"; String address= "Shanghai"; return "The customer name is " + name + " and he is from " + address; } }
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"> <bean id="component" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="restRoute" /> </bean> <bean id="restRoute" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/customers/{custId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="createResource" bean="customerResource" /> </bean> </entry> </map> </property> </bean> <bean id="customerResource" class="com.mycompany.restlet.resource.CustomerResource" scope="prototype"> <property name="customerDAO" ref="customerDAO" /> </bean> <bean id="customerDAO" class="com.mycompany.restlet.dao.CustomerDAOImpl"/> </beans>
ok,所有配置以及代码完成,下面做一个简单的测试,打开浏览器输入: http://localhost:8080/restlet/resources/customers/1
页面结果是:
The customer name is ajax and he is from Shanghai
看上面的Spring配置,如果有多个URL,例如
/customers/{custId},
/customers/{custId}/orders,
/customers/{custId}/orders/{orderId}
那么/customers需要重复三次,有什么办法简化吗?看下面改造后的配置:
<bean id="restRoute" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/customers" value-ref="customerRoute" /> </map> </property> </bean> <bean id="customerRoute" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/{customerId}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="createResource" bean="customerResource" /> </bean> </entry> </map> </property> </bean>
这样,可以动态配置基于/customers/*的URL了,如果你仍旧不明白,看一下官方的restlet与Spring结合的例子。
评论
Error creating bean with name 'component' defined in URL [file:/F:/Tomcat6.0/webapps/RestletSpring/WEB-INF/classes/applicationContext.xml]:
发表评论
-
Restlet实战(三十)(完结篇)运行流程之源代码分析(续)
2009-08-24 14:25 4125前面一篇文章分析了servlet里init方法,包括init方 ... -
Restlet实战(二十九)(完结篇)运行流程之源代码分析
2009-08-20 17:32 5236终于到了完结篇,也体 ... -
Restlet实战(二十八)源代码分析之压缩 (Compression)
2009-08-10 15:36 3203上篇文章我给出了如何 ... -
Restlet实战(二十七)压缩 (Compression)
2009-08-05 12:09 7156在进入代码部分之前,还是贴出<<RESTful W ... -
Restlet实战(二十六)事务 (Transaction)
2009-08-02 23:29 5331<<Restful Web Service> ... -
Restlet实战(二十五)缓存 (Cache)
2009-07-31 22:18 3945说明:以下部分文字说明摘自<<Restful We ... -
Restlet实战(二十四)获取参数值(续)
2009-07-31 14:44 10374这个系列之前已经有一篇文章写如何获取参数值,看Restlet实 ... -
欢迎加入Restlet圈子
2009-07-28 22:28 2760如果你进来是因为想看Restlet相关的文章,那么欢迎你加入r ... -
Restlet实战(二十三)实现条件GET (Conditional Get)
2009-07-28 17:47 5006先普及一下什么是条件GET,以下摘自<<Restf ... -
Restlet实战(二十二)仿造PUT和DELETE
2009-07-28 13:17 7158在Restlet实战(七)-提交和处理Web Form 中提到 ... -
Restlet实战(二十一)如何保护确定的资源(续)
2009-07-16 16:18 3963在Restlet实战(十七)如何保护确定的资源 中我给出一个如 ... -
Restlet实战(二十)使用Restlet之SSL
2009-07-15 21:37 3269待写 -
Restlet实战(十九)使用Restlet实现Web Service
2009-07-15 21:34 4286先说明本篇文章要实现的功能,仍然做一些假设,当前系统是基于Re ... -
Restlet实战(十八)Restlet如何产生WADL
2009-07-11 21:57 9804现在究竟REST是否需要WADL这种东西,有很多争论,有人说不 ... -
Restlet实战(十七)如何保护确定的资源
2009-07-11 21:55 3395在面向资源的架构中, ... -
Restlet实战(十六)结合源代码分析及使用Filter
2009-07-11 21:13 5713其实在Web应用中Filter对大家来说一点都不陌生,比如说在 ... -
Restlet实战(十五)如何与表示层交互
2009-07-10 13:51 5337首先还是设定一个应用场景,看看用restlet如何实现。 ... -
Restlet实战(十四)如何在Restlet得到Servlet request和Session
2009-07-09 16:39 11493如果你现在已经有一个web系统,而为了一些需求,你集成了res ... -
Restlet实战(十三)如何在Servlet中呼叫Restlet
2009-07-09 14:47 4504看到这个题目,或许你会问,你之前的很多文章不都是与servle ... -
Restlet实战(十二)获取参数值
2009-07-07 15:06 6913本篇文章将讲解三种不 ...
相关推荐
Restlet与Spring 集成
org.restlet.ext.spring.jar
com.noelios.restlet.ext.spring_2.5.jar
Restlet2 + Spring3 注解方式配置
简单做了一个rsh框架的集成,知道这个而且需要欢迎下载,大家有需要的下载了给点宝贵意见。
基于Spring的Restlet实例 代码和jar包
NULL 博文链接:https://ajaxcn.iteye.com/blog/438237
cas-server-integration-restlet-3.5.2.jar cglib-nodep-2.1_3.jar ...restlet.ext.spring-1.1.1.jar
com.noelios.restlet.ext.servlet_2.4.jar
org.restlet.ext.slf4j.jar
a sampel with caeml restlet in web app , jetty
restlet相关文档
spring2.5 API 官方网站上下载下来的
org.restlet.jar
restlet项目
RESTLET开发实例(三)基于spring的REST服务
一个简单的Restlet开发框架的Basic认证的例子
Restlet Client插件是一款运行在chrome内核浏览器上的Web服务测试插件,该插件主要用于测试各种Web服务,能查看网站基本信息、浏览网页代码并能发送HTTP请求来测试网站Web服务,同时支持自动化API场景。用户在安装了...
restlet2.1学习笔记项目代码