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

RESTful 项目中对 Cloudant 服务的封装使用(4)
创建 Cloudant 数据库到目前为止,你已经在 Bluemix 上创建了一个基于 liberty profile 的项目,并创建了一个 Cloudant NoSQL 的服务。接下来的问题是如何去管理这个 Cloudant 数据库。Cloudant 提供了一个 web 的 Dashboard,你可以在这个 Dashboard 上面管理你创建的数据库。选择页面左边导航条目中的 SERVICES 下的 Cloudant NoSQL DB 点击右上角的 Launch 按钮。
在 Database 页面的左上角点击 Add New Database,输入 crud 数据库名称,点击 Create 按钮。
图 16.创建 crud 数据库实例 选择 crud 数据库,点击页面左边 All Documents 的加号,创建一个新的文档,如下图 17 所示。
图 17.创建一个文档 Cloudant 服务使用配置- 在本项目中,前端采用 jquery.couch.js 的 jQuery 库,对 Cloudant 数据库进行操作,项目中需要把 jquery-2.1.1.js 和jquer.couch.js 两个 js 文件引入。后端使用了 Apache http jar 包实现 HTTP 请求的转发,需要引入四个 jar包:commons-codec-1.6.jar、commons-logging-1.1.3.jar、httpclient-4.3.6.jar、httpcore-4.3.3.jar。
- 项目中使用读取 properties 文件中的三个属性值:dbhost,username,password。其中 dbhost 表示 Cloudant 数据库的 url,username 和 password 分别是访问数据库的用户名和密码。使用 Properties 的 load 方法读取文件中配置的 Cloudant 数据库属性。
- 初始化 CloseableHttpClient 对象,初始化一个 SSL 链接的可信任客户端 client.
清单 3.初始化 client1
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
| public static CloseableHttpClient getClient() {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
username, password);
provider.setCredentials(AuthScope.ANY, credentials);
SSLContextBuilder builder = new SSLContextBuilder();
try {
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SSLConnectionSocketFactory sslsf = null;
try {
sslsf = new SSLConnectionSocketFactory(builder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return HttpClients.custom().setSSLSocketFactory(sslsf)
.setDefaultCredentialsProvider(provider).build();
}
|
|
|
|
|
|
|