本文的目的在于完成一个Restlet入门示例。
首先,是一个Stand alone应用。
然后,将其部署与Tomcat容器中。
最后,完成Restlet与Spring的整合。
1.按照官方教程,完成“firstSteps”
创建一个动态Web工程RestEE,并建立firstSteps包,并复制如下代码到:HelloWorldResource.java
package firstSteps;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
/**
* Resource which has only one representation.
*/
public class HelloWorldResource extends ServerResource {
@Get
public String represent() {
return "hello, world";
}
}
2.复制如下代码到FirstStepsApplication.java
package firstSteps;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class FirstStepsApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attach("/hello", HelloWorldResource.class);
return router;
}
}
3.编写main,复制如下代码到:FirstStepsMain.java
package firstSteps;
import org.restlet.Component;
import org.restlet.data.Protocol;
public class FirtstStepsMain {
public static void main(String[] args) throws Exception {
// Create a new Component.
Component component = new Component();
// Add a new HTTP server listening on port 8182.
component.getServers().add(Protocol.HTTP, 8182);
// Attach the sample application.
component.getDefaultHost().attach("/firstSteps",
new FirstStepsApplication());
// Start the component.
component.start();
}
}
4.以Java Application运行FirstStepsMain,在浏览器中输入:
http://localhost:8182/hello,将打印“hello, world”信息。
5.修改web.xml,具体代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>firstSteps</display-name>
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>
firstSteps.FirstStepsApplication
</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
6.启动Tomcat,运行该项目。在浏览器中输入:
http://localhost:8080/RestEE/hello,出现“hello, world”信息。
7.集成restlet,spring
7.1 修改web.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>firststepsservlet</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>component</param-value>
</init-param>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
7.2 application.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="component" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="application" />
</bean>
<bean id="application" class="firstSteps.FirstStepsApplication">
<lookup-method name="createRoot" bean="root" />
</bean>
<bean id="root" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/hello">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="HelloWorldResource" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="HelloWorldResource" class="firstSteps.HelloWorldResource" scope="prototype" />
</beans>
7.3 启动Tomcat,运行该项目。浏览器中输入:
http://localhost:8080/RestEE/hello,出现“hello,world”信息。