Restlet 2.0 边学边写(二)发布多个Resource

上一次实践是一个Application绑定一个Resource,很简单就通过了。但是如果想要发布多个Resource怎么办呢?


参考:[url]http://ajaxcn.iteye.com/blog/415093[/url]

本次实践将发布两个Resource,CustomerResource和OrderResource。

1.包
创建包com.sunny.restlet.order。

2.Resource
在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 = "";

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

@Get
public String represent() {
return "get customer id :" + customerId;
}

}

代码中定义了CustomerResource类资源初始化的动作:从request中读取custId属性,和Get方法。

创建OrderResource类,代码如下:

package com.sunny.restlet.order;

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

public class OrderResource extends ServerResource {
String orderId = "";
String subOrderId = "";

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

@Get
public String represent() {
return "the order id is : " + orderId + " and the sub order id is : "
+ subOrderId;
}
}
代码中定义了OrderResource 类资源初始化的动作:从request中读取orderId、subOrderId属性,和Get方法。

3.Application
创建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("/orders/{orderId}/{subOrderId}", OrderResource.class);
router.attach("/customers/{custId}", CustomerResource.class);
return router;

}
}

类中将OrderResource和CustomerResource分别发布到两个路径上。

4.Main
创建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 Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getDefaultHost()
.attach("/firstSteps", new OrderApplication());
component.start();
}
}

类中将Application分别发布到localhost:8182/firstSteps上。

5.运行
运行OrderMain类。
通过浏览器访问[url]http://localhost:8182/firstSteps/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :1
[/list]
访问[url]http://localhost:8182/firstSteps/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : 1 and the sub order id is : 2,
[/list]
两个Resource发布成功。

6.servlet
将web.xml中的

<!-- FirstStepsApplication -->
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>com.sunny.restlet.firstSteps.FirstStepsApplication</param-value>
</init-param>

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

重新部署后(Eclipse中修改web.xml会自动重新部署,稍后即可),通过浏览器访问[url]http://localhost:8080/firstSteps/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :1
[/list]
访问[url]http://localhost:8080/firstSteps/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : 1 and the sub order id is : 2,
[/list]
说明两个Resource在Servlet容器中发布成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值