结合 Dojo 和 JAX-RS 创建 RESTful 服务(2)
 
- UID
- 1066743
|

结合 Dojo 和 JAX-RS 创建 RESTful 服务(2)
服务器端实现浏览器客户端实现已完成,让我们来看看一个 JAX-RS 后端实现的示例。dojox.data.FileStore 有一个协议实现,使数据源存储可以理解从服务器收到的 JSON 内容。除了 JAX-RS,您也可以使用一个 Web 2.0 功能部件包提供的帮助程序实用工具库 JSON4J。JSON4J 库提供一组易于构造 JSON 数据格式的类。
我们来看看 WorldCupGroup 类,它有两个方法,见清单 3。JAX-RS 使用注释来减少样板文件代码并提供一个简要的注释。清单 2 显示了这个类定义。
清单 21
2
3
4
5
6
7
8
9
| @Path("/filestore")
public class WorldCupGroup { … }
清单 3
@GET
@Path("filelist")
@Produces(MediaType.APPLICATION_JSON)
// returns a list of files in JSON format that match the request
public JSONObject getFileList(@QueryParam("path") String path) {
|
在这个例子中,@Path 值与 URI 路径有关。例如,在 RESTful 服务请求(在清单 3 中定义的)/rest/filestore/filelist,/filestore/ 表示到 RESTful 服务的 PATH URI。
清单 3 中定义了您的 RESTful 服务调用的方法。@GET 定义了 HTTP GET 方法,@PATH 进一步细化 URI 使其更具体;在此例中是 /rest/filestore/filelist。@Produces 定义了将要返回的 MIME 媒体类型:在本例中,该方法返回转换成一个 MIME 类型的应用程序 /json 的 JSON 数据,这就是 Dojo 的 dojo.data.FileStore 所预期的。@QueryParam 是 JAX-RS 用来通知要传递给 getFileList 方法的查询参数值。例如,在清单 4 中的 HTTP GET 请求中,@QueryParam (“path”) 值是 C://artifactstore//FIFA World Cup Group。
清单 41
2
| GET http://localhost:8080/rest/filestore/filelist?path=C://artifactstore//FIFA World
Cup Group
|
代码的另一部分包含构造描述文件系统的 JSON 数据流的信息。例如,createFileItem 方法从文件系统中读取信息,然后构造一个稍后返回 dojox.data.FileStore 的 JSON 对象。清单 5 显示了完整的服务器端实现代码。
清单 51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| package com.example.filestore;
import java.io.File;
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
@Path("/filestore")
public class FIFAWorldCup {
public static final String ROOT_DIR = "C:\\artifactstore\\";
// returns a list of files in JSON format that match the request
@GET
@Path("filelist")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject getFileList(@QueryParam("path") String path) {
JSONObject ret = new JSONObject();
JSONArray filelist = new JSONArray();
JSONObject items = new JSONObject();
if (path == null) {
path=this.ROOT_DIR;
}
/** Method to construct a JSONObject representing the File
* as a DataStore item.
*
* @param file The file contained within rootDir to reference.
*
* @return JSONObject of the file, or null if file was null or not
* contained within the root path of the FileStore.
*/
private JSONObject createFileItem(String parent,File file) throws
IOException {
JSONObject item = new JSONObject();
if (parent != null && file != null) {
String filePath = file.getAbsolutePath();
//Make sure the request is contained within the root directory
if (filePath.startsWith(this.ROOT_DIR)){
item.put("name", file.getName());
item.put("modified", new Long(file.lastModified()));
item.put("size", new Long(file.length()));
item.put("path", filePath);
item.put("directory", new Boolean(file.isDirectory()));
// if this is a directory, then add the children
if (file.isDirectory()) {
File[] child = file.listFiles();
JSONArray children = new JSONArray();
if (child.length > 0) {
for (int j = 0; j < child.length; j++) {
children.add(child[j].getName());
item.put("children", children);
}
}
else {
// since this directory has no children, place an empty child
item.put("children",children);
}
}
}
}
return item;
}
} // end of class definition
|
结束语最新版本的 IBM WebSphere Application Server Feature Pack for Web 2.0 提供了创建端到端的 RESTful 服务所需的库。在客户端,您有一个由 Dojo 组成的功能强大的开源 JavaScript 工具箱。对于服务器端实现,您有基于 Apache Wink 项目的 JAX-RS 库。文中给出的代码表示一个简单样例后端实现 dojox.data.FileStore。此外,协议实现可以被添加,例如查询和对于您的数据可能是惟一的用户定义选项。参考下面的参考资料,获取一个有用的链接列表,帮助您以自己的方式使用 Dojo 和 JAX-RS 来创建 RESTful 服务。 |
|
|
|
|
|