RESTful 项目中对 Cloudant 服务的封装使用(6)
 
- UID
- 1066743
|

RESTful 项目中对 Cloudant 服务的封装使用(6)
清单 8.浏览修改文档1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| $('#update').click(function() {
var doc = $.parseJSON('{"_id":"id","_rev":"rev","title":"HTML and CSS: Design'
+'and Build Websites","price":100,"authors":["Jon Duckett"],"language":"html"}');
$.couch.urlPrefix = '../jaxrs/cloudant';
var db = $.couch.db('crud');
db.saveDoc(doc, {
success : function(response, textStatus, jqXHR) {
console.log('success');
// do something if the save works
},
error : function(jqXHR, textStatus, errorThrown) {
// do something else if it goes wrong
console.log('error');
}
});
});
|
设置 cloudant.com 的路径,获取初始化后 CloseableHttpResponse 对象,把浏览器端传过来 JSON 对象转化为 StringEntity 对象,再把其放入 HttpPost,如果 rev 的值存在,设置请求 header 的 If-Match 的值为浏览器的请求参数 rev,如果不存在 rev,那么该操作是带 id 的新建一个文档。并调用 excute 函数。版返回的 httpresponse 转为为 JSON 的封装后的 response。
清单 9.服务器端修改文档1
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
| @PUT
@Produces(value = MediaType.)
@Path(value = "/{db}/{id}")
public Response SaveDocById(@PathParam("db") String db,
@QueryParam("rev") String rev, JSONObject json) {
String responseString = null;
HttpPut httpPut = new HttpPut(cloudantURL + db);
System..println(cloudantURL + db);
CloseableHttpResponse response = null;
CloseableHttpClient client = getClient();
try {
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json");
httpPut.setEntity(entity);
httpPut.setHeader("Connection", "keep-alive");
if(null != rev && !"".equals(rev)){
httpPut.setHeader("If-Match", rev);
}
response = client.execute(httpPut);
HttpEntity ReEntity = response.getEntity();
responseString = EntityUtils.toString(ReEntity, "UTF-8");
int status = response.getStatusLine().getStatusCode();
Header[] headers = response.getAllHeaders();
client.close();
return httpResponse2JsonRespone(status, responseString, headers);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System..println(responseString);
return Response.serverError().build();;
}
|
删除一个文档,首先设置 web 服务器的访问访问 Cloudant 数据库路径,指定对应的访问数据库,对应文档的_id 和_rev 的值,并转为 Json 格式的。调用 revomeDoc 方法传入 doc 和请求返回之后的回调函数。
清单 10.浏览器端删除文档1
2
3
4
5
6
7
8
9
10
11
12
13
| $('#delete').click(function() {
var doc =$.parseJSON('{"_id": id,"_rev":rev}');
$.couch.urlPrefix = '../jaxrs/cloudant';
var db = $.couch.db('crud');
db.removeDoc(doc, {
success : function(response, textStatus, jqXHR) {
console.log(response);
},
error : function(jqXHR, textStatus, errorThrown) {
// do something else if it goes wrong
}
});
});
|
设置 cloudant.com 的路径,获取初始化后 CloseableHttpResponse 对象,把浏览器端传过来 JSON 对象转化为 StringEntity 对象,再把其放入 HttpPost,设置请求 header 的 If-Match 的值为浏览器的请求参数 rev,并调用 excute 函数。把返回的 httpresponse 转为 JSON 封装后的 response。
清单 11.服务器端删除文档1
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
| @DELETE
@Produces(value = MediaType.APPLICATION_JSON)
@Path(value = "/{db}/{docid}")
public Response deleteDocById(@PathParam("db") String db,
@PathParam("docid") String docid, @QueryParam("rev") String rev) {
HttpDelete httpDelete = new HttpDelete(cloudantURL + db + "/" + docid);
System.out.println(cloudantURL + db + "/" + docid);
httpDelete.setHeader("X-SyncTimeOut", "300");
httpDelete.setHeader("If-Match", rev);
HttpResponse response = null;
CloseableHttpClient client = getClient();
try {
response = client.execute(httpDelete);
HttpEntity ReEntity = response.getEntity();
String responseString = EntityUtils.toString(ReEntity, "UTF-8");
int status = response.getStatusLine().getStatusCode();
Header[] headers = response.getAllHeaders();
client.close();
return httpResponse2JsonRespone(status, responseString, headers);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
|
|
|
|
|
|
|