上篇文章结合了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,会怎么样:
相关推荐
这个文件名可能指的是项目的主要源代码目录,包含Camel、Spring和Restlet的配置、路由定义、控制器等。其中可能有以下关键组件: - **pom.xml**:项目对象模型文件,定义了项目依赖和构建过程。 - **src/main/...
- **依赖注入**:通过Spring的DI,可以在不修改代码的情况下改变服务的行为,如更换数据源、添加日志记录等。 - **AOP支持**:可以利用Spring的AOP功能实现事务管理、权限控制和异常处理。 - **测试友好**:由于服务...
接下来,我们需要创建一个`restlet-servlet.xml`文件,该文件用于配置Spring容器中的REST资源映射。 ```xml <lookup-method name="create" bean="StudentResource"/> <lookup-method name=...
- **整合Hibernate**:在Spring配置文件中,我们需要定义Hibernate的数据源、SessionFactory以及事务管理器。然后,可以使用Spring的JPA或Hibernate特定的DAO(数据访问对象)来操作数据库,Spring的`@Transactional...
Spring 2.5 对测试框架进行了改进,引入了 @ContextConfiguration 和 @RunWith 注解,方便进行基于注解的测试,支持对 Spring 容器的控制和测试数据源的管理。 十、其他改进 Spring 2.5 还包含了对其他方面的改进,...
配置Spring、SpringMVC 配置logback日志输出 安装PE助手和Restlet Client (3)用户模块 用户登陆 用户退出 用户注册 获取用户登录信息 忘记密码 更新用户个人信息 获取用户个人信息 (4)商品模块 获取商品详情 商品...
- Jersey是JAX-RS规范的主要实现,是一个开放源代码的RESTful框架,自2012年开始进入2.x版本。 - 它提供了一套全面的API来创建和消费RESTful服务,支持多种内容类型和扩展。 - Jersey具有高度可定制性和灵活性,...