如何使用spring RestTemplate对Cloudant API发出测试请求?

时间:2015-06-05 13:02:32

标签: resttemplate cloudant

我正在寻找使用Spring RestTemplate对Cloudant执行基本API调用的一些指导。

以下是一些假设:

但是,我不确定如何开始将CloudTemplate与Cloudant一起使用。例如,是否存在可用于测试连接并返回结果集的基本Cloudant API调用?

1 个答案:

答案 0 :(得分:0)

以下示例显示如何使用Cloudant Education的示例数据库对Cloudant进行基本调用。

import org.springframework.web.client.RestTemplate;

public class Application {

    private final static String URL = 
        "https://education.cloudant.com/animaldb/_design/views101/_view/latin_name?include_docs=true";

    public static void main(String args[]) {

        RestTemplate restTemplate = new RestTemplate();

        // Normally you would bind the response to Java domain objects and not String
        // See https://spring.io/guides/gs/consuming-rest/ for examples of binding 
        // response objects to Java domain objects

        String animals = restTemplate.getForObject(URL, String.class);

        System.out.println(animals);
    }
}

你应该看到一些类似的输出:

...
{
  "total_rows": 5,
  "offset": 0,
  "rows": [
    {
      "id": "kookaburra",
      "key": "Dacelo novaeguineae",
      "value": 19,
      "doc": {
        "_id": "kookaburra",
        "_rev": "4-6038cf35dfe1211f85484dec951142c7",
        "min_length": 0.28,
        "max_length": 0.42,
        "wiki_page": "http:\/\/en.wikipedia.org\/wiki\/Kookaburra",
        "class": "bird",
        "diet": "carnivore",
        "latin_name": "Dacelo novaeguineae"
      }
    },
    ... 
  ]
}
...

注意