本文描述利用通过 org.restlet.ext.servlet.ServerServlet ,实现到Tomcat环境的部署。有两种实现方式,分别如下:
所需jar包:org.restlet.ext.servlet .jar、org.restlet.jar
1、用 Application实现
项目结构如下:
1)、创建两个继承 ServerResource的类
FirstServerResource.java:
package test; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; //定义一个资源 public class FirstServerResource extends ServerResource { @Get public String toString() { return "Hello World !"; } }
SecondServerResource.java:
package test; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; //定义一个资源 public class SecondServerResource extends ServerResource { @Get public String toString() { return "two***!"; } }
2)、创建一个继承 Application 的类(ServerApplication.java):
ServerApplication.java:
package test; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; //为资源配置路径 public class ServerApplication extends Application { @Override public Restlet createInboundRoot() { Router router = new Router(this.getContext()); //绑定资源路径到对应的处理资源类 router.attach("/first", FirstServerResource.class); router.attach("/second", SecondServerResource.class); return router; } }
3)、修改web.xmlview plain
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>org.restlet.application</param-name> <!-- 自定义org.restlet.Application扩展类 --> <param-value>test.ServerApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
4)、启动Tomcat,分别通过http://localhost:8182/testRestLet01-2/first、http://localhost:8182/testRestLet01-2/second访问。
2、用Component实现
项目结构如下:
1)、创建两个继承 ServerResource的类(FirstServerResource.java、SecondServerResource.java):方法同上
2)、创建两个继承 Application 的类(FirstServerApplication.java、SecondServerApplication.java):
FirstServerApplication.java:
package application; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; import resource.FirstServerResource; //为资源配置路径 public class FirstServerApplication extends Application { @Override public Restlet createInboundRoot() { Router router = new Router(this.getContext()); //绑定资源路径到对应的处理资源类 router.attach("/one", FirstServerResource.class); return router; } }
SecondServerApplication.java:
package application; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; import resource.SecondServerResource; //为资源配置路径 public class SecondServerApplication extends Application { @Override public Restlet createInboundRoot() { Router router = new Router(this.getContext()); //绑定资源路径到对应的处理资源类 router.attach("/two", SecondServerResource.class); return router; } }
3)、创建一个继承 Component 的类(MyComponent.java):
MyComponent.java:
package component; import org.restlet.Component; import application.FirstServerApplication; import application.SecondServerApplication; public class MyComponent extends Component{ /** * 重写createInboundRoot通过attach方法绑定资源类,并且制定了访问路径 * */ public MyComponent(){ getDefaultHost().attach("/first", new FirstServerApplication()); getDefaultHost().attach("/second", new SecondServerApplication()); } }
4)、配置 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>org.restlet.component</param-name> <param-value>component.MyComponent</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
5)、部署后,通过 localhost:8182/testRestLet01-2/first/one 和 localhost:8182/testRestLet01-2/Second/two访问。
注:在restlet中一个component可以对应多个application,一个application可以对应多个resource