参考: http://ajaxcn.iteye.com/blog/415093
本次实践将发布两个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类。
通过浏览器访问 http://localhost:8182/firstSteps/customers/1可以看到提示信息
- get customer id :1
访问 http://localhost:8182/firstSteps/orders/1/2可以看到提示信息
- the order id is : 1 and the sub order id is : 2,
两个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会自动重新部署,稍后即可),通过浏览器访问 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,
说明两个Resource在Servlet容器中发布成功。