4、restlet 2.3 用戶指南(四) spring


原文鏈接:http://restlet.com/technical-resources/restlet-framework/guide/2.3

以下翻譯,以直譯方式爲主,內容主要針對目前應用最爲廣泛的基於 Java 的服務器端的開發與應用,並修復原文中涉及到的代碼 bug 。


PS:下文中resources、representation、RESTful 等屬於一組相關的領域內專有名詞,不便翻譯。其中 resources 可以理解爲“資源”。representation 可以理解爲“表現層對象”。RESTful 可以理解爲基於 REST 理念或架構的框架和應用。


測試環境:

Mac OS X Yosemite Version 10.10.4

IntelliJ IDEA 14.app jdk1.8.0_51.jdk

maven 3.3.3

4、介紹

下文描述的是怎麼樣把 maven 和 spring 集成到 restlet 框架之中的一種大概示範,並不是 restlet 框架的使用教程。


PS:由於官網上例子不完整,下面內容由個人整合,提供 RestletFrameworkServlet 以及 SpringServerServlet 配置方式作爲參考例子,後者由官方例子修改並附帶官方翻譯。


4.1 org.restlet.ext.spring.RestletFrameworkServlet


實現過程源碼簡要說明:

spring 初始化容器,加載

org.restlet.ext.spring.RestletFrameworkServlet 

initFrameworkServlet() 執行框架初始化

--getTargetRestlet() 

通過 targetRestletBeanName 參數路由到對應的 restlet  Application(通過 byName 方式,默認就是 byName 方式)

----getTargetRestletBeanName() 

讀取 -servlet.xml 配置,獲取 targetRestletBeanName 的值(String 類型)。當 targetRestletBeanName 爲空的時候,默認返回“root”。所以在不配置該參數的情況下(一般都不配置),在 -servlet.xml 中必須有一個名爲“root”的 bean,該 bean 必須直接指向一個 Application 或者可以路由到一組 Application ,否則服務器啓動時候將提示沒有找到或者沒有實現 root 的 restlet。

由於 root 直接指向單個 application 的方式應用範圍很小,更多采用指向 org.restlet.ext.spring.SpringBeanRouter 方式封裝一組 resource 形成 application 後由容器加載。下文將以後者爲例。


4.1.1 目錄結構:



4.1.2 pom.xml 主要依賴



    <properties>
        <restlet.version>2.3.4</restlet.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.servlet</artifactId>
            <version>${restlet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.spring</artifactId>
            <version>${restlet.version}</version>
        </dependency>
    </dependencies>


依賴結構圖:



4.1.3 WEB-INF/web.xml



<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


    <display-name>restletGuideSample</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>



4.1.4 WEB-INF/dispatcher-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="root" class="org.restlet.ext.spring.SpringBeanRouter"/>
    <bean id="/test1" class="com.simon.framework.restletGuideSample.BaseCampResource"/>
    <bean id="/test2" class="com.simon.framework.restletGuideSample.BaseCampResource"/>
</beans>


4.1.5 實現類

com.simon.framework.restletGuideSample.BaseCampResource


package com.simon.framework.restletGuideSample;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * Created by HEAVEN on 7/31/15.
 */
public class BaseCampResource extends ServerResource {

    @Get("txt")
    public String getResource() {
        return "hello, world!";
    }
}


測試:
http://localhost:8888/restletGuideSample/test1 
http://localhost:8888/restletGuideSample/test2 
輸出:hello, world!


4.2 org.restlet.ext.spring.SpringServerServlet(官方)

目錄結構:


pom.xml 同上

PS:因爲 WEB-INF/dispatcher-servlet.xml 文件常見用於 spring mvc 模式下的配置,爲日後方便進行混合模式的開發,保留該文件,清空該文件的配置。


4.2.1 WEB-INF/web.xml



<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <context-param>
        <param-name>org.restlet.component</param-name>
        <param-value>springComponent</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


    <display-name>restletGuideSample</display-name>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>


4.2.2 WEB-INF/applicationContext.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="restlet-config.xml"/>
    <import resource="restlet-componentResource.xml"/>

</beans>


4.2.3 WEB-INF/restlet-config.xml

用於 restlet 配置,其中:

router :默認的路由器,框架核心的一部分,決定了 URLs 是如何映射到 resource 。
baseCampApplication :應用,關聯到 router 。
springComponent :spring 的一個封裝類,核心類,關聯到 application 。(PS:根據 restlet API文檔,這是一個很有意思的組件類,通過該組件,可以方便地引入 restlet 對分佈式集羣應用的支持,並且可以看出 restlet 對該方面靈活、簡便而強大的特性。但同時需要注意,該類很自然地不是線程安全的,繼承的子類需要小心使用)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="router" name="router" class="org.restlet.ext.spring.SpringBeanRouter"/>

    <bean id="springComponent" name="springComponent" class="org.restlet.ext.spring.SpringComponent">
        <property name="defaultTarget" ref="baseCampApplication"/>
    </bean>

    <bean id="baseCampApplication" name="baseCampApplication"
          class="com.simon.framework.restletGuideSample.BaseCampApplication">
        <property name="inboundRoot" ref="router"/>
    </bean>

</beans>



4.2.4 WEB-INF/restlet-componentResource.xml

用於 restlet 資源配置

/test1、/test2 :關聯到resource 的類。該 bean 的 id 是非必要的。根據 web.xml 中 dispatch-servlet.xml 的配置,任何“/”開頭的 bean 都被設定爲資源的路由地址,並且相應的 bean 會被註冊到路由器 router 中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="test1" name="/test1" class="com.simon.framework.restletGuideSample.BaseCampResource"/>
    <bean id="test2" name="/test2" class="com.simon.framework.restletGuideSample.BaseCampResource"/>

</beans>


4.2.5 實現類 com.simon.framework.restletGuideSample.BaseCampApplication


這個類對於本例子來說不是必要的。但當你需要重載基類的行爲,這裏顯示如何實現。


package com.simon.framework.restletGuideSample;

import org.restlet.Application;

/**
 * Created by HEAVEN on 7/31/15.
 * Project : restletSample-management
 */
public class BaseCampApplication extends Application {

}



4.2.6 實現類 com.simon.framework.restletGuideSample.BaseCampResource

這是一個最簡單的 resource 實現,繼承了 ServerResource,響應 HTTP GET 方法。注意,這裏使用的註解,是 restlet 框架的一部分。


package com.simon.framework.restletGuideSample;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * Created by HEAVEN on 7/31/15.
 * Project : restletSample-management
 */
public class BaseCampResource extends ServerResource {
    @Get("txt")
    public String toString() {
        return "hello, world!";
    }
}


測試:
http://localhost:8888/restletGuideSample/test1 
http://localhost:8888/restletGuideSample/test2 
輸出:hello, world!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章