首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Apache Wink REST 开发中的高级主题(3)

Apache Wink REST 开发中的高级主题(3)

资源和提供者注册加载了 Spring 上下文之后,您可以使用 Apache Wink 提供的 org.apache.wink.spring.Registrar 类注册资源和提供者。Registrar 类是我们熟悉的 WinkApplication 类的扩展,首先必须定义为 spring bean。                               
还可以使用 Registrar 类定义以下属性:                               
  • 实例。资源和提供者实例通常定义为 Spring bean,可以自动获取 IoC 以及其他 Spring 功能。
  • 类。定义资源和提供者的类名;该属性与 Wink                                                         Application 类中的 getClasses 方法相同。
  • 优先级。该属性定义 WinkApplication 的优先级。
是 Spring bean 配置文件的一个示例,将 Apache Wink 资源和提供者定义为 Spring bean。                               
清单 5. Spring 配置文件 mySpringcontext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<bean class="org.apache.wink.spring.Registrar">
  <property name="classes">
    <set value-type="java.lang.Class">
      <value>org.openengine.example.TransactionResource</value>
    </set>
  </property>
  <property name="instances">
    <set>
      <ref bean="resources.TransactionResource"/>
      <ref bean="providers.myprovider"/>
    </set>
  </property>
</bean>




将 Wink 作为客户端除了是遵从 JAX-RS 的服务器框架外,Apache Wink 还可以作为高级客户端框架。Apache Wink 客户端框架提供一个简单的 Java API,完成实现客户端的任务以直接使用基于 HTTP 的 RESTful                         Web 服务。Apache Wink 客户端框架还有一个可自定义的处理器机制,用于操作 HTTP 请求和响应。Apache Wink 客户端以 JAX-RS 原则为基础构建,包括基于 REST 的概念和标准。通过将 REST 驱动的构想映射到 Java 类,它们有助于为基于 Apache Wink 的服务和任何 HTTP 驱动的                         RESTful Web 服务开发客户端。作为一个独立的基于 REST 的                         Java 客户端框架,它们非常有用。               
以下是 Apache Wink 客户端框架的一些主要功能:
  • 使用可配置的 JAX-RS 提供者序列化和反序列化资源
  • 对各种内容类型提供内置支持,包括 Atom、JSON、RSS、APP、CSV 和 Multipart,以及 Java 中对应的对象模型
  • 提供对 Secure Sockets Layer (SSL) 和 HTTP 代理的支持
  • 类似于服务器端框架,客户端框架有一个可配置的处理程序链,用于操作 HTTP 请求和响应
  • 默认使用 java.net.HttpUrlConnection 类提供核心 HTTP 传输
  • 允许轻松定制核心 HTTP 传输机制
中的图表演示了 Apache Wink 客户端框架的架构。               
图 1. Apache Wink 客户端架构正如您看到的,Apache Wink 客户端框架主要包括 RestClient 类,该类是保存各种配置和提供者注册的中央位置。要开始处理客户端框架,必须实例化一个新的 RestClient 对象实例。然后,使用希望连接的服务的 URI 从中创建 Resource 类的一个实例。Resource 类是与特定 URI 有关的 RESTful Web 资源的 Java 等效类,用于执行基于 HTTP 的操作。所有的 HTTP 方法调用都通过可自定义的处理程序链进行筛选,处理程序链可以轻松操作 HTTP 请求和响应。               
GET 请求 演示了通过 Apache Wink 客户端使用 HTTP GET 请求的示例。                       
清单 6. GET 请求示例
1
2
3
4
5
6
7
8
// create the rest client instance
RestClient client = new RestClient();

// create the resource instance to interact with
Resource resource = client.resource("http://localhost:8080/HelloWorld");

// perform a GET on the resource. The resource will be returned as plain text
String response = resource.accept("text/plain").get(String.class);




如前所述,RestClient 对象是 Apache Wink 客户端框架的起点。要构建 RESTful Web 服务客户端,您必须实例化一个新的 RestClient 对象实例,之后使用希望通过 RestClient#resource() 方法调用的服务的 URI 创建一个新的 Resource 对象。要发布 HTTP                                GET 请求,您必须调用 Resource#get() 方法。该方法返回 HTTP 响应。然后,通过调用相应的提供者,该客户端还可以对响应进行反序列化。                       
POST 请求 演示了通过 Apache Wink 客户端使用 HTTP POST                                请求的示例。                       
清单 7. POST 请求示例
1
2
3
4
5
6
7
8
9
10
// create the rest client instance
RestClient client = new RestClient();

// create the resource instance to interact with
Resource resource = client.resource("http://localhost:8080 ");

// issue the request
String response = resource.contentType("text/plain").
            accept("text/plain").
                post(String.class, "foo");




从中可以看到,发出 POST 请求类似于发出 GET 请求。就像 GET 请求示例一样,您通过 RestClient 创建一个新的 Resource 实例。唯一的不同在于,指定请求和响应媒体以及响应实体类型之后,POST 字符串本身作为方法参数传递到 resource.post 方法。同样,该响应以字符串形式返回。                       
Atom 客户端请求如上所述,Apache Wink 客户端框架还提供一些以多种内容类型发出请求的功能。 提供了一个示例,演示如何发出发送和接受 Atom 项的 HTTP POST 请求。                       
清单 8. Atom 请求示例
1
2
3
4
5
6
7
8
9
10
11
// create the rest client instance
RestClient client = new RestClient();

// create the resource instance to interact with
Resource resource = client.resource("http://services.co");
AtomEntry request = getAtomEntry();

// issue the request
AtomEntry response = resource.contentType("application/atom+xml").
            accept("application/atom+xml").
                post(AtomEntry.class, request);




由于 Apache Wink 客户端为支持发送和接受 Atom feed 和 Atom 项的 Atom 提供对象模型,所以使用 AtomEntry 对象发出 Atom 请求和解析响应很简单。
返回列表