生活随笔
收集整理的這篇文章主要介紹了
Restlet入门示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://cunfu.iteye.com/blog/757467
本文的目的在于完成一個Restlet入門示例。 首先,是一個Stand alone應用。 然后,將其部署與Tomcat容器中。 最后,完成Restlet與Spring的整合。 1.按照官方教程,完成“firstSteps” 創建一個動態Web工程RestEE,并建立firstSteps包,并復制如下代碼到:HelloWorldResource.java
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; ???? ? ? ?? public ?class ?HelloWorldResource?extends ?ServerResource?{ ???? ????@Get ?? ????public ?String?represent()?{ ?? ????????return ?"hello,?world" ; ?? ????} ?? ?? }??
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 {@Getpublic String represent() {return "hello, world";}}
2.復制如下代碼到FirstStepsApplication.java
Java代碼 ?
package ?firstSteps; ???? import ?org.restlet.Application; ??import ?org.restlet.Restlet; ??import ?org.restlet.routing.Router; ???? public ?class ?FirstStepsApplication?extends ?Application?{ ???? ????? ? ?? ????@Override ?? ????public ?synchronized ?Restlet?createInboundRoot()?{ ?? ?????????? ????????Router?router?=?new ?Router(getContext()); ?? ?? ?????????? ????????router.attach("/hello" ,?HelloWorldResource.class ); ?? ?? ????????return ?router; ?? ????} ?? ?? }??
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.*/@Overridepublic 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 routerouter.attach("/hello", HelloWorldResource.class);return router;}}
3.編寫main,復制如下代碼到:FirstStepsMain.java
Java代碼 ?
package ?firstSteps; ???? import ?org.restlet.Component; ??import ?org.restlet.data.Protocol; ???? public ?class ?FirtstStepsMain?{ ??????public ?static ?void ?main(String[]?args)?throws ?Exception?{?? ?? ???????? ?? ????????Component?component?=?new ?Component();?? ?? ?????? ?? ?????????? ????????component.getServers().add(Protocol.HTTP,?8182 );?? ?? ?????? ?? ?????????? ????????component.getDefaultHost().attach("/firstSteps" ,?? ?? ????????????????new ?FirstStepsApplication());?? ?? ?????? ?? ?????????? ????????component.start();?? ?? ????}? ?? }??
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代碼 ?
<? 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 > ?? ????? ?? ???< 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 > ??
<?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代碼 ?
<? 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 > ?? ?? ??? ?? ????? ?? ????< 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 > ?? ?? ?? ?? ????? ?? ???< servlet-mapping > ?? ?? ??????< servlet-name > RestletServlet</ servlet-name > ?? ?? ??????< url-pattern > /*</ url-pattern > ?? ?? ???</ servlet-mapping > ?? ?? </ web-app > ??
<?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代碼 ?
<? 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 > ??
<?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”信息。
總結
以上是生活随笔 為你收集整理的Restlet入门示例 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。