Restlet 2.2提供了Servlet和Spring的扩展,可以简单方便的将Restlet部署到Tomcat等环境中。
本文描述利用Restlet 2.2 Servlet扩展,实现到Tomcat环境的部署。
一、通过org.restlet.ext.servlet.ServerServlet 部署
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
<context-param> <param-name>org.restlet.application</param-name> <param-value>org.teamlet.rest.component.ComponentApplication</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>/rest/*</url-pattern> </servlet-mapping>
4)、部署后,通过 http://127.0.0.1:8080/myservice/rest/hello 访问
2、用Component实现
1)、创建两个继承 ServerResource的类:方法同上
2)、创建两个继承 Application 的类:方法同上,resource类与相应的application对应
3)、创建一个继承 Component 的类: (注:这里one 和two 绑定到不同的application即可,我这里写的是同一个)
package org.teamlet.rest.component;public class ComponentComponent extends org.restlet.Component { public ComponentComponent() { getDefaultHost().attach("/one", new ComponentApplication()); getDefaultHost().attach("/two", new ComponentApplication()); } }
<init-param> <param-name>org.restlet.component</param-name> <param-value>org.teamlet.rest.component.ComponentComponent</param-value> </init-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>/rest/*</url-pattern> </servlet-mapping>
5)、部署后,通过 http://127.0.0.1:8080/myservice/rest/one 和 http://127.0.0.1:8080/myservice/rest/two访问
二、通过org.restlet.ext.servlet.ServletAdapter 部署
1、创建一个servlet类,继承HttpServlet
2、在servlet中创建一个Restlet实例trace,在handle方法中重写逻辑
public class TestServlet extends HttpServlet { private ServletAdapter adapter; public void init() throws ServletException { super.init(); this.adapter = new ServletAdapter(getServletContext()); Restlet trace = new Restlet(this.adapter.getContext()) { public void handle(Request req, Response res) { getLogger().info("Hello World"); res.setEntity("Hello World!", MediaType.TEXT_PLAIN); } }; this.adapter.setNext(trace); } protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { this.adapter.service(req, res); } }
3、配置 web.xml
<servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.teamlet.rest.component.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>