spring集成restlet1
来源:互联网 发布:mac预览图片怎么删除 编辑:程序博客网 时间:2024/05/21 17:20
首先将相应的jar文件放到WEB-INF/lib下,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">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
接下来对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) {
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.resource.CustomerResource" scope="prototype">
<property name="customerDAO" ref="customerDAO" />
</bean>
<bean id="customerDAO" class="com.dao.impl.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结合的例子。
原文地址:http://ajaxcn.javaeye.com/blog/416913
- spring集成restlet1
- Spring集成
- spring spring mvc集成
- Spring Boot集成Spring Data
- 【spring】spring 与 redis集成
- 【Spring】Struts和Spring集成
- Spring MVC + Spring + Hibernate集成
- spring boot 集成spring security
- spring boot 集成spring security
- 集成Spring和Axis
- JSF和Spring集成
- axis + spring 集成
- axis + spring 集成(2)
- JSF和Spring集成
- JSF和Spring集成
- [webservices开发]集成Spring
- 集成JSF和Spring
- Tapestry3 与 Spring 集成
- VB 文件读写笔记
- hosts配置不生效问题
- oracle temp表空间增长过快的解决方法
- cisco ios 升级方法和TFTP的用法
- c# 编写 最简单的 webService 例子
- spring集成restlet1
- 热门话题:使用WCF实现SOA面向服务编程(一)——SOA的概念
- linux(ubuntu) USB转串口使用minicon
- pthread_cond_wait()太难理解了
- Delphi2010中的fastMM
- VB.NET和C#之间的语法不同比较,已经更新到.NET4.0了
- Outlook 2007的桌面提醒
- DirectShow抓取视频帧 ISampleGrabber
- 麒麟远创面试题1:随机播放CD