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

本文档详细介绍了如何将Restlet 2.3框架与Spring进行集成,提供了从目录结构到具体配置文件的实例解析。主要内容包括RestletFrameworkServlet的使用,SpringBeanRouter的配置,以及一个简单的BaseCampResource实现,展示了如何响应HTTP GET请求。通过这些步骤,读者可以了解到如何在Spring环境中搭建和使用Restlet应用。
摘要由CSDN通过智能技术生成


原文链接: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!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值