1、在WS-AppServer Package中新建自定义类“C_MongoPackage”

2、添加自定义方法Method,expandDocument和findDocuments两个方法。

3、方法设置两个参数,分别是collectionName和JSONString。

4、由系统产生Java代码

5、弹出如下“Generate Java Code on WS-AppServer Models”窗口。

6、由系统生产WebService

7、弹出Generate WebService Interface on WS-AppServer Models窗口。

接下来,分别发布Java代码和WebService。
8、上传MongoDB Java Driver包到系统上平台上。

9、添加Java程序依赖Jar包,鼠标双击Java文件下的“XiaoywWSAppServerPackage”包,弹出“WS-AppServer Java Archive Definiton”窗口,在“Dependencies”页面,通过“+”增加Jar包。此Jar包选自JARLIB里的mogo-java-driver-2.12.4.jar文件。

10、把系统生成Java代码上传SVN。

11、同步代码到Eclipse工程中。
12、在Eclipse中,完善如下代码
package com.unicom.xiaoywwsappserverpackage;
import com.cordys.cpc.bsf.busobject.BusObjectConfig;
import com.cordys.cpc.bsf.busobject.BusObjectIterator;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.util.JSON;
public class C_MongoPackage extends C_MongoPackageBase
{
......
//输入参数,collectionName:文档名称,JSONString:MongoDB所识别JSON数据格式数据字符串
? ? public static void expandDocument(String collectionName,String JSONString)
? ? {
? ? ? ? DB db = null; ?
? ? ? ? try { ?
? ? ? ? ? ???MongoClientURI uri = new MongoClientURI("mongodb://username:password@10.64.52.120:27017/dbname");
//username:数据库用户名、 password:密码、 ? dbname:数据库名称
? ? ? ? ? ???MongoClient client = ?new MongoClient(uri); ? ? ?
? ? ? ? ? ???db = client.getDB(dbname);
? ? ? ? ? ? ?DBCollection collection = db.getCollection(collectionName);
? ? ? ? ? ? ?
? ? ? ? ? ? ?DBObject query = (BasicDBObject) JSON.parse(JSONString);
? ? ? ? ? ? ?
? ? ? ? ? ? ?collection.save(query);
? ? ? ? } catch (Exception e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } finally{
? ? ? ? if (db!=null){
? ? ? ? try{
? ? ? ? ? ? ? ? db.requestDone();
? ? ? ? ? ? ? ? db=null;
? ? ? ? }catch(Exception e) { ?
?
? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ?
? ? ? ? }
? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
13、在Eclipse中,通过Team->提交带回到平台中,再“Java - Publish to Organization”,编译发布代码到平台中使用。
14、测试界面如下:

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
? <SOAP:Body>
? ? <ExpandDocument xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
? ? ? <collectionName>BusinessEntity</collectionName>
? ? ? <JSONString>{"ID":"2015040010","Name":"采购流程","Dept":"经营管理部"}</JSONString>
? ? </ExpandDocument>
? </SOAP:Body>
</SOAP:Envelope>
15、数据库插入结果如下所示:
