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

How can I use spring RestTemplate to make a test request against the Cloudant API?

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

这里有一些假设:

但是,我不确定如何开始将 RestTemplate 与 Cloudant 结合使用。例如,是否有一个基本的 Cloudant API 调用可用于测试连接性和 return 结果集?

以下示例向您展示了如何使用 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"
      }
    },
    ... 
  ]
}
...

注意