本文描述利用Restlet 2.2 Spring扩展,实现到Tomcat环境的部署。
一、利用 org.restlet.ext.spring.RestletFrameworkServlet 部署
1、使用 Application
1)、创建一个继承 ServerResource的类
package org.teamlet.rest.component;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
public class ComponentResource extends ServerResource {
@Get
public String represent() {
return "hello, world";
}
}
2)、创建一个继承 Application 的类
package org.teamlet.rest.component;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class ComponentApplication extends Application {
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/hello", ComponentResource.class);
return router;
}
}
3)、配置web.xml
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
对应web.xml中的servlet-name,创建一个xml文件,名字为 restlet-servlet.xml (restlet就是在web.xml中的servlet的名字)。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean name="root" class="org.teamlet.rest.component.ComponentApplication"/>
</beans>
5)、部署启动后,访问 http://127.0.0.1:8080/myservice/rest/hello
使用Application这种方式太简单,下面的更适合大规模开发使用。
2、使用org.restlet.ext.spring.SpringRouter
1)、上述配置不变的情况下,修改 restlet-servlet.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean name="application" class="org.teamlet.rest.component.ComponentApplication"/>
<bean name="root" class="org.restlet.ext.spring.SpringRouter">
<constructor-arg ref="application" />
<property name="attachments">
<map>
<entry key="/one" value="org.teamlet.rest.component.ComponentResource" />
<entry key="/two" value="org.teamlet.rest.component.ComponentResource" />
</map> </property> </bean></beans>
2)、部署后访问 http://127.0.0.1:8080/myservice/rest/one 和http://127.0.0.1:8080/myservice/rest/two
需要注意的是,原来application中的/hello 并没有加载,所有的加载都在 SpringRouter的属性attachments中。
3、使用org.restlet.ext.spring.SpringBeanRouter
1)、上述配置不变的情况下,修改 restlet-servlet.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean name="root" class="org.restlet.ext.spring.SpringBeanRouter"/>
<bean name="/one" id="one" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
<bean name="/two" id="two" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
</beans>
2)、部署后访问 http://127.0.0.1:8080/myservice/rest/one 和http://127.0.0.1:8080/myservice/rest/two
这样可以任意配置多个uri,每个uri可以动态的增加属性和引用其他bean。
因此,这个是最方便和灵活的!
二、利用org.restlet.ext.spring.SpringServerServlet 部署
上面虽然都可以实现restlet的资源与uri的映射,但是如果需要对component、application中的属性做设置就比较困难。
SpringServerServlet 提供了更加灵活的方式。
前面的CompoentApplication、ComponentResource 都不做任何修改。
1、修改 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">
<display-name>Restful Webservice Component</display-name>
<description>Restful Webservice Component</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/application-context.xml
</param-value>
<description>Spring config file locations</description>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>application</param-value>
</init-param>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>component</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/restlet/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、在WEB-INF目录下的 classes中创建config 目录
3、在config目录中 创建application-context.xml 文件,内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<import resource="classpath:config/rest/rest-component-config.xml"/>
<import resource="classpath:config/extension/*-context.xml"/>
</beans>
rest-component-config.xml 是为配置 restlet使用的文件,其他扩展的配置文件可以直接放入extension中(这个目录没有,需要的时候自己加)不用修改任何配置直接生效。
4、在config目录下创建 rest 目录
5、在 rest目录下创建rest-component-config.xml 文件,内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean name="component" id="component" autowire="byName" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="application" />
</bean>
<bean name="application" id="application" class="org.teamlet.rest.component.ComponentApplication">
<property name="inboundRoot" ref="router" />
</bean>
<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter"></bean>
<bean name="/one" id="one" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
<bean name="/two" id="two" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
<bean name="/three" id="three" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
<bean name="/four" id="four" autowire="byName" class="org.teamlet.rest.component.ComponentResource" />
</beans>
6、部署之后,访问 http://127.0.0.1:8080/myservice/restlet/one 即可!
用这种方法部署restlet,可以根据需要定制 compoent、application、router等,提供的不同类型的服务。