`
sb33060418
  • 浏览: 151770 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Restlet 2.0 边学边写(三)使用Component发布多个Application

    博客分类:
  • REST
阅读更多
很久没更新这篇博客了,今天继续。

上一次实践是一个Application绑定多个Resource。但是如果Resource多了以后使用一个Application来发布并不合适,而且绑定地址写在代码中也不便于修改。那么应该怎么办呢?
可以使用Component来发布多个Application,每个Application只负责发布自己的Resource。使用Component有两种方法:可以自定义Component类并在web.xml中配置;也可以使用restlet.xml来配置。

参考:http://ajaxcn.iteye.com/blog/416611

本次实践将创建一个Application:CustomerApplication;一个Component:OrderComponent,一个配置文件:restlet.xml。

1.Application
在com.sunny.restlet.order包中创建CustomerApplication类,代码如下:
package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class CustomerApplication extends Application {

	@Override
	public Restlet createRoot() {
		// TODO Auto-generated method stub
		Router router = new Router(getContext());
		router.attach("", CustomerResource.class);
		return router;
	}

}

类中将CustomerResource绑定到""路径上。

修改com.sunny.restlet.order包中OrderApplication类,代码如下:
package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;


public class OrderApplication extends Application {
	
	@Override
	public synchronized Restlet createRoot() {
		Router router = new Router(getContext());
		router.attach("", OrderResource.class);		return router;

	}
}

类中删除CustomerResource的发布路径,并将OrderResource绑定到""路径上。

2.Component
在com.sunny.restlet.order包中创建OrderComponent类,代码如下:
package com.sunny.restlet.order;

import org.restlet.Component;

public class OrderComponent extends Component {

	public OrderComponent() {
		super();
		// TODO Auto-generated constructor stub
		getDefaultHost()
				.attach("/orders/{orderId}/{subOrderId}", new OrderApplication());
		getDefaultHost().attach("/customers/{custId}",
				new CustomerApplication());
	}

}

OrderComponent将OrderApplication和CustomerApplication发布到新的路径上。

3.Main
修改com.sunny.restlet.order包中OrderMain类,代码如下:
package com.sunny.restlet.order;

import org.restlet.Component;
import org.restlet.data.Protocol;

public class OrderMain {
	public static void main(String[] args) throws Exception {
		Component component = new OrderComponent();
		component.getServers().add(Protocol.HTTP, 8182);
		component.start();
	}
}

类中将使用OrderComponent来发布资源。

4.运行
运行com.sunny.restlet.order包中OrderMain类。
通过浏览器访问http://localhost:8182/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8182/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

证明使用Component发布Application和两个Resource成功。

5.web.xml
将web.xml中的
<!-- OrderApplication -->
<init-param>
	<param-name>org.restlet.component</param-name>
	<param-value>com.sunny.restlet.order.OrderApplication</param-value>
</init-param>

替换为
<!-- OrderComponent -->
<init-param>
	<param-name>org.restlet.component</param-name>
	<param-value>com.sunny.restlet.order.OrderComponent</param-value>
</init-param>

配置文件中将使用OrderComponent来发布资源。

6.运行
重新部署后(Eclipse中修改web.xml会自动重新部署,稍后即可),通过浏览器访问http://localhost:8080/firstSteps/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8080/firstSteps/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

说明在Servlet容器中使用Component类发布Application和两个Resource成功。

7.web.xml
将web.xml中以下代码注释掉
<!-- OrderComponent -->
<init-param>
	<param-name>org.restlet.component</param-name>
	<param-value>com.sunny.restlet.order.OrderComponent</param-value>
</init-param>

重新部署后访问页面,报404错误,说明找不到页面,后台创建ServerServlet时出错。
参考org.restlet.ext.servlet.jar库中org.restlet.ext.servlet.ServerServlet类的代码
protected Component createComponent()
{
    Component component = null;
    Client warClient = createWarClient(new Context(), getServletConfig());
    Response response = warClient.handle(new Request(Method.GET, "war:///WEB-INF/restlet.xml"));
    if(response.getStatus().isSuccess() && response.isEntityAvailable())
        component = new Component(response.getEntity());
    if(component == null)
    {
        String componentClassName = getInitParameter("org.restlet.component", null);
        if(componentClassName != null)

restlet创建Component时先读取WEB-INF/restlet.xml文件中配置,读取失败才从web.xml中读取org.restlet.component这个initParameter。

8.restlet.xml
在WEB-INF/目录下创建restlet.xml文件,代码如下:
<?xml version="1.0"?>
<component xmlns="http://www.restlet.org/schemas/1.1/Component"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.restlet.org/schemas/1.1/Component">

	<defaultHost>
		<attach uriPattern="/customers/{custId}"
			targetClass="com.sunny.restlet.order.CustomerApplication" />
		<attach uriPattern="/orders/{orderId}/{subOrderId}"
			targetClass="com.sunny.restlet.order.OrderApplication" />
	</defaultHost>
</component>

文件中将两个Application直接绑定到原有路径上,并不需要创建Component类。

9.运行
重新部署后,通过浏览器访问http://localhost:8080/firstSteps/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8080/firstSteps/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

说明在Servlet容器中使用restlet.xml发布Application和两个Resource成功。

10.进一步思考
如果我们想要访问某个客户的某次订单中的单件商品,例如http://localhost:8080/firstSteps/customers/1/orders/1/2这种资源时,又该如何配置呢?
我们可以将CustomerApplication发布到"/customers/{custId}",将CustomerResource发布到"("/orders/{orderId}/{subOrderId}"路径上,通过路径匹配来实现此项功能。

修改com.sunny.restlet.order包中的CustomerApplication类,代码如下:
package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class CustomerApplication extends Application {

	@Override
	public Restlet createRoot() {
		// TODO Auto-generated method stub
		Router router = new Router(getContext());
		router.attach("", CustomerResource.class);
		router.attach("/orders/{orderId}/{subOrderId}", CustomerResource.class);
		return router;
	}

}

类中将CustomerResource再次绑定到"("/orders/{orderId}/{subOrderId}"路径上。

修改com.sunny.restlet.order包中的CustomerResource类,代码如下:
package com.sunny.restlet.order;

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

public class CustomerResource extends ServerResource {
	String customerId = "";
	String orderId = "";
	String subOrderId = "";

	@Override
	protected void doInit() throws ResourceException {
		this.customerId = (String) getRequest().getAttributes().get("custId");
		this.orderId = (String) getRequest().getAttributes().get("orderId");
		this.subOrderId = (String) getRequest().getAttributes().get(
				"subOrderId");
	}

	@Get
	public String represent() {
		return "get customer id :" + customerId + " and  the order id is : " + orderId + " and the sub order id is : "
		+ subOrderId;
	}

}

类中将读取orderId和subOrderId属性。

重新部署后,通过浏览器访问http://localhost:8080/firstSteps/customers/1/orders/1/2可以看到提示信息
  • get customer id :1 and  the order id is : 1 and the sub order id is : 2

说明路径匹配成功。


分享到:
评论
1 楼 gaofeng_867 2013-02-19  
感谢

相关推荐

    Restlet 2.0 边学边写(八)使用jQuery和ajax实现对Restlet资源的CRUD操作

    至于"firstSteps"这个文件名,可能是该教程中的一个起点项目或者示例代码,它可能包含了一个简单的Restlet应用和使用jQuery进行CRUD操作的前端示例。为了深入学习,你可以下载这个压缩包,查看并运行其中的代码,...

    restlet2.0版本jee源代码

    Restlet是一个开源框架,专为构建RESTful(Representational State Transfer)Web服务而设计。REST是一种轻量级的架构风格,常用于构建互联网应用程序。在Restlet 2.0版本中,它支持Java Enterprise Edition (Java ...

    restlet2.0+spring3.0+hibernate3.3.框架集成

    在这个场景中,我们关注的是"restlet2.0+spring3.0+hibernate3.3"的整合,这是一个经典的Java Web开发组合,分别代表了RESTful API、服务层管理和持久化层的优秀实践。 首先,让我们深入了解每个框架的核心特性: ...

    restlet-jee-2.0.6.zip_Restlet 2..0_Restlet framework2.0_org.rest

    标题"restlet-jee-2.0.6.zip_Restlet 2..0_Restlet framework2.0_org.rest"表明这是一个针对Java企业版(Java EE)的Restlet框架2.0.6版本的压缩包,其中包含了与org.restlet相关的组件。 描述中的"restlet框架所需...

    Restlet开发实例

    在Restlet框架中,Component是整个应用的基础,它负责管理和协调多个Application。Application则是实际处理HTTP请求的实体,它可以包含多个不同的资源。通过Component和Application,你可以更好地组织和管理REST服务...

    RESTLET开发

    RESTLET提供了多个版本以适应不同平台的需求,例如Java SE、Java EE、Android等。本节将介绍如何使用JEE(Java Enterprise Edition)版本的RESTLET进行开发。 1. **下载RESTLET JEE版本** 首先,需要从官方网站...

    restlet实现最简单的restful webservice

    它将Web服务的组件分解为三个主要部分:代表(Representations)、资源(Resources)和代理(Clients)。资源是RESTful服务的核心,它们响应HTTP请求并返回相应的表示。 要使用Restlet实现一个最简单的RESTful Web...

    RESTLET开发(三)

    ### RESTLET开发(三):基于Spring的REST服务 #### 一、基于Spring配置的Rest简单服务 在本文档中,我们将深入探讨如何利用RESTlet框架与Spring框架结合,构建高效的RESTful服务。Spring框架因其强大的功能和灵活...

    Restlet学习的三篇文章

    Restlet是一个开源框架,专为构建RESTful(Representational State Transfer)Web服务而设计。REST是一种轻量级的架构风格,常用于构建高效、可扩展的网络应用程序。它强调资源的概念,通过URI(统一资源标识符)来...

    Restlet所需要的所有jar包

    Restlet是一款开源的Java框架,专门用于构建RESTful(Representational State Transfer)Web服务。REST是一种轻量级的架构风格,常用于构建高效、可扩展的网络应用程序。本压缩包包含Restlet框架运行所需的全部jar...

    restlet

    RESTlet是一款开源框架,专为构建基于REST(Representational State Transfer)原则的Web服务和应用程序设计。REST是一种轻量级的架构风格,广泛应用于互联网应用的开发,它强调通过简单的HTTP方法(如GET、POST、...

    Restlet in action 英文 完整版

    第二章“Beginning a Restlet application”中,作者通过一个实际的例子引导读者逐步创建一个简单的Restlet应用。这包括设置开发环境、定义资源、处理HTTP方法(如GET、POST等)以及配置服务器。这一章节对于初学者...

    restlet2.1学习笔记项目代码

    Restlet是一个开源框架,专为构建RESTful(Representational State Transfer)Web服务而设计。REST是一种轻量级的架构风格,常用于构建可扩展、高性能的互联网应用程序。本项目是针对Restlet 2.1版本的学习笔记,...

    Restlet与Spring 集成

    - **restletContext.xml**:这是Restlet的配置文件,定义了一个Spring组件`SpringComponent`,并设置了默认目标`defaultTarget`为`application`。`BaseApplication`类是自定义的Restlet应用程序,它使用`component....

    restlet restful

    "绝对不坑"可能意味着这个项目或者RESTlet框架在使用过程中相对稳定,没有太多隐藏的陷阱或者难以理解的问题,开发者可以放心使用。 在提供的压缩包文件名称列表中,只有一个名为"RestApplication"的文件或目录。这...

    restlet入门示例

    Restlet是一个开源框架,专为构建RESTful(Representational State Transfer)Web服务而设计。REST是一种轻量级的架构风格,常用于构建Web应用程序和API,强调简洁、可扩展性和可伸缩性。本示例将带你入门Restlet,...

Global site tag (gtag.js) - Google Analytics