spring集成restlet1

首先将相应的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

你可能感兴趣的:(Restlet)