上篇文章结合了Restlet的源码分析了Restlet-spring的配置文件,并提出了相关的问题,本篇将对这一问题做一个测试解答。
首先修改一下Spring的配置文件:
<bean id="restRoute" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/customers">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="customersResource" />
</bean>
</entry>
<entry key="/customers/{customerId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="createResource" bean="customerResource" />
</bean>
</entry>
<entry key="/users">
<bean class="com.mycompany.restlet.application.UserApplication"/>
</entry>
</map>
</property>
</bean>
除了配置customer外,还配置了一个跟User相关的Application:
public class UserApplication extends Application{
@Override
public synchronized Restlet createRoot() {
Router router = new Router(getContext());
router.attach("/{userId}", UserResource.class);
router.attach("/{userId}/orders", UserOrdersResource.class);
return router;
}
}
为了测试更加的清晰,不在Customer相关类上面做修改了,另外建立两个资源文件:UserResource和UserOrdersResource,对应的URL应该分别是:/users/{userId}和/users/{userId}/orders,第一个URL意思是根据id查询得到某个user,而第二个则是查询得到某个user对应的所有的order。
public class UserResource extends Resource {
String userId;
public UserResource(Context context, Request request, Response response) {
super(context, request, response);
userId = (String) request.getAttributes().get("userId");
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
@Override
public Representation represent(Variant variant) {
String userMsg = "current user id is " + userId;
Representation representation = new StringRepresentation(userMsg,
MediaType.TEXT_PLAIN);
return representation;
}
}
public class UserOrdersResource extends Resource {
String userId;
public UserOrdersResource(Context context, Request request, Response response) {
super(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
userId = (String) request.getAttributes().get("userId");
}
@Override
public Representation represent(Variant variant) {
String userMsg = "get all orders for user whose id is: " + userId;
Representation representation = new StringRepresentation(userMsg,
MediaType.TEXT_PLAIN);
return representation;
}
}
需要注意的是,上面两个资源是直接attach到Application,而不是之前文章讲的那样由SpringFinder接管,所以,之前文章强调一些规则,如需要有一个无参的构造函数,一个init方法等,就不适用于上述的两个资源的定义。
没有别的代码,来测试一下吧,打开浏览器,输入http://localhsot:8080/restlet/resources/users/1后,页面会显示:
current user id is 1
而输入http://localhost:8080/restlet/resources/users/1/orders 后,页面会显示:
get all orders for user whose id is: 1
我们接着测试,如果value是String,会怎么样:
分享到:
相关推荐
a sampel with caeml restlet in web app , jetty
restlet2.0版本jee源码,例子,jar包,api等内容
Restlet与Spring 集成
chromecj.com-Restlet-Client-REST-API-Testing_v2.8.0.1非常实用的谷歌插件
restlet-client 谷歌插件 接口调试 最新
Restlet2 + Spring3 注解方式配置
restlet2.1学习笔记项目代码
chromecj.com-Restlet-Client-REST-API-Testing_v2.8.0.1.crx
restlet源代码,描述了restlet框架中每个类的具体实现
org.restlet.ext.spring.jar
Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务;而且,事实证明它自从2005年诞生之时起,就是一个可靠的软件。 ...
restlet_client插件,不需要按照postman就可以实现restful api 调试。 支持浏览器:360浏览器、360极速浏览器、谷歌浏览器等使用谷歌内核的浏览器。
Web Api调试工具 Chrome插件Restlet-Client-REST-API-Testing
Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务;而且,事实证明它自从2005年诞生之时起,就是一个可靠的软件。 ...
基于Spring的Restlet实例 代码和jar包
Restlet Client - REST API Testing(DHC)详情:https://blog.csdn.net/dream_weave
做restlet web开发所必须的jar包。
RESTlet-jee-2.0.9自用.rar RESTlet-jee-2.0.9自用.rar
安装步骤:1、打开在谷歌浏览器--》右上角的三个点--》更多工具--》扩展程序,将已下载的restlet client插件拖拽至此即可 2、谷歌浏览器右上角的有一个“R”的图标,点击即可打开restlet client工具使用
座便器电影 Restlet WS基于JWSUR源代码的修改。