构建一个集成了 Web 服务的可移植 Java 旅行应用程序(2)
 
- UID
- 1066743
|

构建一个集成了 Web 服务的可移植 Java 旅行应用程序(2)
步骤 4. 绑定一个云服务 (MongoDB)要使用 MongoDB 服务,必须先创建一个服务实例:
- 连接到 Bluemix 并在操作视图中选择 Add a Service。 图 4. Bluemix dashboard of applications
 - 从可用服务列表中选择 MongoDB,然后创建服务实例。 图 5. 创建用于 UserService 应用程序的 MongoDB 实例服务
 另外还可以使用命令行:
1
2
3
4
5
| cf create-service mongodb 100 mongodb_ser1
(USAGE: cf create-service SERVICE PLAN SERVICE_INSTANCE)
cf bind-service UserService mongodb_ser1
(USAGE: cf bind-service APP SERVICE_INSTANCE)
|
现在,MongoDB 服务已准备就绪并被绑定到 UserService。
步骤 5. 在应用程序中使用 MongoDB 服务创建服务并将它关联到应用程序之后,将它的配置作为一个只读环境变量添加到 VCAP_SERVICES,其中包含您在代码中连接服务时需要使用的信息。在这个例子中,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| {
"mongodb-2.2": [
{
"name": "mongodb-ser1",
"label": "mongodb-2.2",
"plan": "100",
"credentials": {
"hostname": "10.0.116.106",
"host": "10.0.116.106",
"port": 10192,
"username": "46c538a6-e6e1-4d02-8132-77b0a4b2dc1c",
"password": "0ceea0ea-5548-46ad-9b09-1002683aeca7",
"name": "946dc87b-b455-4d12-b977-1b1ee22f1ade",
"db": "db",
"url": "mongodb://46c538a6-e6e1-4d02-8132-77b0a4b2dc1c:
0ceea0ea-5548-46ad-9b09-1002683aeca7@10.0.116.106:10192/db"
}
}
]
}
|
要使用 VCAP_SERVICES 变量连接到 MongoDB 服务实例,提取 "url" JsonNode。请注意,该 URL 包含连接数据库需要的所有参数(用户证书、主机名、端口和 DB 名称)。
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
| private static String getUrlConnection() {
String env = System.getenv("VCAP_SERVICES");
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode node = mapper.readTree(env);
Iterator<JsonNode> dbNode = node.get("mongodb-2.2").getElements();
JsonNode cred = (JsonNode)dbNode.next().get("credentials");
String uri =cred.get("url").getTextValue();
logger.debug ("url db: " + uri);
return uri;
} catch (JsonGenerationException e) {
logger.debug(e.getMessage());
} catch (JsonMappingException e) {
logger.debug (e.getMessage());
} catch (IOException e) {
logger.debug (e.getMessage());
}
return null;
}
|
可以使用 MongoDB Java 驱动程序创建一个 Spring 配置类,用于创建 DB 连接,然后将 DB 对象返回给客户端。
1
2
3
4
5
6
7
8
9
| @Configuration
public class MongoConfiguration {
public @Bean DB mongoDb() throws Exception {
MongoClientURI mcUri = new MongoClientURI(getUrlConnection());
MongoClient mc = new MongoClient(mcUri);
mc.getDB(mcUri.getDatabase());
return mc.getDB(mcUri.getDatabase());
}
|
UserManager 类使用 MongoConfiguration 与 DB 进行交互。init 方法获取 "users" 集合,或者在该集合不存在时创建一个集合。
1
2
3
4
5
6
| private void init(){
ApplicationContext ctx =
new AnnotationConfigApplicationContext(MongoConfiguration.class);
db = (DB) ctx.getBean("mongoDb");
coll = db.getCollection("users");
}
|
MongoDB 并非关系 DBMS,而是面向文档的。这意味着我们拥有的不是表而是连接,不是行(或值组)而是文档,是字段而不是列。这些字段不是预先定义好的,这点和表中的列一样。您可以在一个集合中输入任意类型的数据。要找到文档必须创建 BasicDBObject。
1
2
3
| BasicDBObject user = new BasicDBObject("username", userData.getUsername());
return (DBObject)coll.findOne(user);
|
UserController 是 UserManager 的客户端, 它使用它的函数去获取和保存已登录的用户信息。
1
2
3
4
5
6
7
8
9
10
| @RequestMapping(value="/user", method = RequestMethod.GET)
public @ResponseBody String getUser(@RequestParam("username") String username,
@RequestParam("password") String password) {
logger.debug("BEGIN: controller getUser - username:" + username);
UserManager userManager = new UserManager();
BasicDBObject user = (BasicDBObject) userManager.getUser(username, password);
logger.debug("END: controller getUser - user:" + user);
return user.toString();
}
|
|
|
|
|
|
|