在上一篇文章中介绍了如何在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结合的例子。