Restlet 2.2 provides Servlet and spring extensions, allowing you to easily deploy restlet to Tomcat and other environments.
This article describes how to use restlet 2.2 spring extension to deploy Tomcat environment.
1. Use org. restlet. Ext. Spring. restletframeworkservlet to deploy
Restletframeworkservlet inherits the frameworkservlet class in spring. When spring is started, the initframeworkservlet () method is called.
1. Use Application
1) create a class that inherits 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) create a class that inherits the 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) Configure 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>
4), configure restlet-servlet.xml
Corresponds to the servlet-name in Web. XML, creates an XML file named restlet-servlet.xml (restlet is the name of the servlet in Web. 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.teamlet.rest.component.ComponentApplication"/> </beans>
5) after the deployment is started, access http: // 127.0.0.1: 8080/myservice/rest/Hello
Using application is too simple. The following method is more suitable for large-scale development.
2. Use org. restlet. Ext. Spring. springrouter
1), the above configuration remains unchanged, modify the 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) Access http: // 127.0.0.1: 8080/myservice/rest/One and http: // 127.0.0.1: 8080/myservice/rest/two after deployment
Note that/hello in the original application is not loaded, and all the loads are in the springrouter attribute attachments.
3. Use org. restlet. Ext. Spring. springbeanrouter
1), the above configuration remains unchanged, modify the 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) Access http: // 127.0.0.1: 8080/myservice/rest/One and http: // 127.0.0.1: 8080/myservice/rest/two after deployment
In this way, multiple Uris can be configured at will, and each URI can dynamically add attributes and reference other beans.
Therefore, this is the most convenient and flexible!
2. Use org. restlet. Ext. Spring. springserverservlet for deployment
Although the above functions can map restlet resources to Uris, it is difficult to set attributes in component and application.
Springserverservlet provides a more flexible approach.
The preceding compoentapplication and componentresource are not modified.
1. modify 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. Create the config directory in the classes under the WEB-INF directory
3. Create a application-context.xml file in the config directory with the following content:
<?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>
The rest-component-config.xml is the file used to configure restlet, and other extension configuration files can be directly put into extension (this directory does not exist, you need to add it yourself) without modifying any configuration to take effect directly.
4. Create a rest directory under the config directory
5. Create a rest-component-config.xml file in the rest directory with the following content:
<?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. After deployment, access http: // 127.0.0.1: 8080/myservice/restlet/one!
To deploy restlet in this way, you can customize compoent, application, and router as needed to provide different types of services.