如何使用gwt处理couchdb的db连接?

时间:2014-05-05 07:31:48

标签: gwt couchdb

我是couchdb的新手,我想了解如何在我们的gwt服务器端程序中连接couchdb。到目前为止,我尝试使用它的gui创建数据库添加文档并向其添加字段。但我无法在程序中使用它。到底是做什么的.. 我尝试了一些代码,但没有得到它。

2 个答案:

答案 0 :(得分:1)

在你的GWT中,你的服务器应该有这样的东西。除此之外,您应该为您的实体(此处发生erktorp)以及将GWT客户端与服务器连接的机制(例如RequestFactory)。(/ p>)

  //Object of your own related with couch db management
  CouchDbAccess couchDbAccess = null;

  @Inject
  public CouchDbManagement(String ddbbUrl, String ddbbName) throws IOException {
    HttpClient httpClient;
    Builder b;
    try {
      b = new StdHttpClient.Builder().url(ddbbUrl);  
    } catch (Exception e) {
      e.printStackTrace();
      ddbbUrl = "http://admin:sa@localhost:5984";
      b = new StdHttpClient.Builder();
    }

    b.socketTimeout(60000);

    String user = getUserFrom(ddbbUrl);
    String pass = getPassFrom(ddbbUrl);
    b.username(user).password(pass);

    httpClient = b.build();


    CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
    if (initialize && dbInstance.getAllDatabases().contains(ddbbName)) {
      dbInstance.deleteDatabase(ddbbName);
      dbInstance = new StdCouchDbInstance(httpClient);
    }

    //If you want Lucene, here is the place

    db.createDatabaseIfNotExists();
    new IndexUploader().updateSearchFunctionIfNecessary(db, ...);
    new IndexUploader().updateSearchFunctionIfNecessary(db, ...);

    URI dbURI = URI.prototype(DbPath.fromString(ddbbName).getPath());
    RestTemplate restTemplate = new RestTemplate(dbInstance.getConnection());

    couchDbAccess = new CouchDbAccess(db, dbURI, restTemplate);
  }

答案 1 :(得分:0)

Couchdb对它的api有一个安静的界面。一切都可以通过网址获得

http://localhost:5984/db_name/doc_name

实际上整个http api都是documented in the wiki。现在我不熟悉gwt,但每个框架都有http个库,您可以使用这些库来调用couchdb http端点。

快速谷歌搜索给了我this资源,可以指导您如何通过gwt创建http请求。